我如何修改代码以使所有语句都能工作? [英] How I can modify the code to make work all statements?

查看:17
本文介绍了我如何修改代码以使所有语句都能工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个代码:

private void Install_Click(object sender, EventArgs e)
{

    string configfilename = path + "config.ini";
    string installerfilename = path + "installer.ini";

    string configtext = File.ReadAllText(configfilename);
    string installertext = File.ReadAllText(installerfilename);

    var link = File.ReadLines(path + "config.ini").ToArray();
    var lin = File.ReadLines(path + "installer.ini").ToArray();

    foreach (var txt in link)
    {

        if (txt.Contains("PLP="))
        {
            var PLPPath = txt.Split('=')[1];
            installertext = installertext.Replace("fileInstallationKey=", "fileInstallationKey=" + PLPPath);
        }
        else if (txt.Contains("Output="))
        {
            var outputPath = txt.Split('=')[1];
            installertext = installertext.Replace("outlog=", "outlog=" + outputPath);
        }
        else
        {

            var license = lin.Select(line => Regex.Replace(line, @"license=.*", "license=yes"));
            File.WriteAllLines(installerfilename, license);

            var lmgr_files = lin.Select(line => Regex.Replace(line, @"lmgr_files=.*", "lmgr_files=false"));
            File.WriteAllLines(installerfilename, lmgr_files);

            var lmgr_service = lin.Select(line => Regex.Replace(line, @"lmgr_service=.*", "lmgr_service=false"));
            File.WriteAllLines(installerfilename, lmgr_service);
        }
        File.WriteAllText(installerfilename, installertext);
    }

但问题是此代码从 installer.ini 中的 config.ini 复制数据,从 PLP 行输出行.而在 installer.ini 中的行:licenselmgr_fileslmgr_service 未完成数据:falsefalse.换句话说 who is after else 不起作用.如何修改代码以执行所有语句?

But the problem is this code copies the data from config.ini in installer.ini, from the PLP row, Output row. And in installer.ini the rows :license, lmgr_files, lmgr_service are not completed with data : yes, false, false. With other words who is after else doesn't work . How I can modify the code to make all statements execute?

installer.ini中我有很多东西,但最重要的是:

In installer.ini I have many things but the most important are :

license=false //I want to have license=yes
lmgr_files=   //I want to have lmgr_files= 
lmgr_service=  
fileInstallationKey=0000000  //the number from config.ini
outlog="jhaoif"              //the code from config.ini

推荐答案

试试这个代码.

string configfilename = path + "config.ini";
string installerfilename = path + "installer.ini";

var link = File.ReadLines(path + "config.ini").ToArray();
var lin = File.ReadLines(path + "installer.ini").ToArray();
IEnumerable<string> newInstaller = lin;
foreach (var txt in link)
 {

     if (txt.Contains("PLP="))
     {
        var PLPPath = txt.Split('=')[1];
        newInstaller = newInstaller.Select(line => Regex.Replace(line, @"fileInstallationKey=.*", "fileInstallationKey=" + PLPPath));                   
     }
     if (txt.Contains("Output="))
     {
         var outputPath = txt.Split('=')[1];
         newInstaller = newInstaller.Select(line => Regex.Replace(line, @"outlog=.*", "outlog=" + outputPath));  
     }
 }
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"license=.*", "license=yes"));
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"lmgr_files=.*", "lmgr_files=false"));
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"lmgr_service=.*", "lmgr_service=true"));

string strWrite = string.Join(Environment.NewLine, newInstaller.ToArray());

File.WriteAllText(installerfilename,strWrite);

这篇关于我如何修改代码以使所有语句都能工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆