我如何修改代码,使工作的所有语句

本文关键字:工作 语句 代码 何修改 修改 | 更新日期: 2023-09-27 18:13:59

我有下一个代码:

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.ini,从PLP row, Output行。在installer.ini中,license, lmgr_files, lmgr_service行没有数据:yes, false, false。换句话说,追求别人的人是行不通的。如何修改代码使所有语句执行?

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

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