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

我有下一个代码:

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

试试这个代码。

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

有一种读/写ini文件的机制。 这里没有必要重新发明轮子。 看看读/写INI文件