0

I have the next code :

foreach (var te in lin)
{

    if (te.Contains("license=") && te.Contains("license=false" ))
    {

        installertext = installertext.Replace("license=false", "license= " + "true");

    }
    else if (te.Contains("license="))
    {

        installertext = installertext.Replace("license=", "license=" + "true");

    }

    File.WriteAllText(installerfilename, installertext);
}

and in text file in row who containt license= appear license=truetrue. How I can put the condition to verify if the row is empty or make this code to write just license=true? I tried to put a break; but if I do this license= remain license=

MaticDiba
  • 895
  • 1
  • 11
  • 19
ben
  • 135
  • 8

3 Answers3

1

If you want to replace

  "license=false" => "license=true"
  "license="      => "license=true"

Side note: you don't expect to ran into "license=true", do you? You can use Linq:

  var target = lin
    .Select(line => line.Contains("license=") 
      ? line.Contains("license=false") 
          ? line.Replace("license=false", "license=true")
          : line.Replace("license=", "license=true")
      : line);

  File.WriteAllLines(installerfilename, target);

Edit: if you want to replace whateever "license=sometext":

 var target = lin
   .Select(line => Regex.Replace(line, @"license=.*", "license=true"));

 File.WriteAllLines(installerfilename, target);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • and If I want to replace any, not matter what I have after '=', with license=true, how I can do this ? – ben Sep 22 '15 at 07:09
  • @ben: in case you want to replace `license=SomeText` into `license=true` (e.g. `license=`, `license=true`, `license=false`, `license=bla-bla-bla`) you can use *regular expressions*, see my edit – Dmitry Bychenko Sep 22 '15 at 07:17
  • As I can see you know a lot of things, It's to mach for you to help me with this question http://stackoverflow.com/questions/32689764/how-i-can-call-the-installer-ini-arguments-in-a-silent-installer ? – ben Sep 22 '15 at 07:43
  • I tried to put this code in my program, but doesn't work. you can help me please ? 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 09:28
0

If you are reading text file line by line, like with StreamReader, then you can check if line is empty or if equals "license=" whitout true.

using(StreamReader sr = new StreamReader("Filename"))
{

    while (sr.EndOfStream)
    {
        string line = sr.ReadLine();
        if(line.Trim().Length != 0 && line.Trim().Equals("license="))
        {
            //Do whatever you need to do
        }
    }
}
MaticDiba
  • 895
  • 1
  • 11
  • 19
0

I couldn't understand much since provided code is less but what i observed is that you are setting license=true everytime. So better do it like this:

 if ((te.Contains("license=")) && (te.Contains("license=false" )))
 {
        installertext = installertext.Replace("license=false", "license=true");
 }
 if (te.Contains("license="))
 {
        installertext = installertext.Replace("license=", "license=true");
 }
Amar
  • 407
  • 1
  • 5
  • 24