How I can modify the code to make work all statements?

回眸只為那壹抹淺笑 提交于 2019-12-31 07:29:28

问题


I have the next code :

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

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?

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

回答1:


Try this code.

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



回答2:


There is a mechanism to read/write to ini file. There is no need to re-invent the wheel here. Take a look at Reading/writing an INI file



来源:https://stackoverflow.com/questions/32731966/how-i-can-modify-the-code-to-make-work-all-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!