我如何修改代码以使所有语句都可以工作?

时间:2015-09-23 05:53:54

标签: c# visual-studio-2012

我有下一个代码:

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);
    }

但问题是此代码会复制config.ini中来自installer.iniPLP row行的Output数据。在installer.ini行中licenselmgr_fileslmgr_service未完成数据:yesfalsefalse 。 换句话说,谁在其他人之后并没有工作。 我如何修改代码以使所有语句都执行?

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

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

2 个答案:

答案 0 :(得分:1)

有一种读取/写入ini文件的机制。这里没有必要重新发明轮子。看一眼 Reading/writing an INI file

答案 1 :(得分:1)

试试这段代码。

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);