0

If I put in my code next statements work:

private void Install_Click(object sender, EventArgs e)
        {
var lin =File.ReadLines(path + "installer.ini").ToArray();
var license = lin.Select(line => Regex.Replace(line, @"license=.*", "license=yes"));
            File.WriteAllLines(installerfilename, license);
}

in installer.ini I will have :license=yes. But if I add another one, , just the second one will be work .

private void Install_Click(object sender, EventArgs e)
            {
    var lin =File.ReadLines(path + "installer.ini").ToArray();
    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=true"));
            File.WriteAllLines(installerfilename, lmgr_files);
    }

In installer.ini remain license=no and will be lmgr_files=true . How I can make the second code to work, and way doesn't work?

Micke
  • 2,251
  • 5
  • 33
  • 48
ben
  • 135
  • 8

2 Answers2

1

That's becuase you are reading the file once, writing it twice.

First you are editing the license row, writing the edited file. Then you are editing the lmgr_files row, overwriting your previous edit.

Remove your first call to File.WriteAllLines(). In your second select, use license (i.e. what the first Select() returned) instead of lin (i.e. the original content of the file).

// Use Path.Combine() to combine path parts.
var lin = File.ReadLines(Path.Combine(path, "installer.ini")).ToArray();

// Replace the license=... part. License will now hold the edited file.
var license = lin.Select(line => Regex.Replace(line, @"license=.*", "license=yes"));

// No need to write the file here, as it will be overwritten.
//File.WriteAllLines(installerfilename, license);

// Select from the edited lines (i.e. "license").
var lmgr_files = license.Select(line => Regex.Replace(line, @"lmgr_files=.*", "lmgr_files=true"));

// Now it is time to write!
File.WriteAllLines(installerfilename, lmgr_files);

Optionally, use a different method for editing INI files.

Community
  • 1
  • 1
Micke
  • 2,251
  • 5
  • 33
  • 48
  • Thank you ! and you can help me, please, with this question http://stackoverflow.com/questions/32731966/how-i-can-modify-the-code-to-make-work-all-statements?noredirect=1#comment53309508_32731966 – ben Sep 23 '15 at 12:00
  • Isn't that the exact same question? – Micke Sep 23 '15 at 12:09
  • no, there I have the first part in which I copy some data from another file text – ben Sep 23 '15 at 12:33
  • and here If I want to add another one statement, I tried to put this: var lmgr_service = lmgr_files.Select(line => Regex.Replace(line, @"lmgr_service=.*", "lmgr_service=false")); File.WriteAllLines(installerfilename, lmgr_service); but doesn't work – ben Sep 23 '15 at 12:35
1

You can also do it in one loop. Something like that:

       var lin = File.ReadLines(Path.Combine(path,"installer.ini")).ToArray();
       var license = lin.Select(line =>
       {
           line = Regex.Replace(line, @"license=.*", "license=yes");
           //you can simply add here more regex replacements
           //line = Regex.Replace(line, @"somethingElse=.*", "somethingElse=yes");

           return Regex.Replace(line, @"lmgr_files=.*", "lmgr_files=true");
       });

       File.WriteAllLines(installerfilename, license);
  • your answer help me so mach, and the same thing you can help me with this http://stackoverflow.com/questions/32731966/how-i-can-modify-the-code-to-make-work-all-statements?noredirect=1#comment53309508_32731966the difference is just the fact that I have another statement who help me to copy some data from another file, and I want to make a conection between this – ben Sep 23 '15 at 12:48