0

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
Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
ben
  • 135
  • 8

2 Answers2

1

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

Community
  • 1
  • 1
Viet Nguyen
  • 406
  • 3
  • 12
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);