c# - Writing to the end of a file if two things don't match -


        counter = 0;         string line;         bool validcheck = true;           // read file , display line line.         system.io.streamreader file =            new system.io.streamreader(deckfile);         while ((line = file.readline()) != null && validcheck == true)         {             if (line.split(',')[1] == form1.tempusernameout)             {                 validcheck = false;             }             else             {                 if (file.endofstream)                 {                     int linecountdecks = file.readalllines(deckfile).length + 1; // makes variable set amount of lines in deck file + 1.                     string deckwriting = linecountdecks.tostring() + "," + form1.tempusernameout + ",1,,,,,,,,,,,2,,,,,,,,,,,3,,,,,,,,,,,4,,,,,,,,,,,5,,,,,,,,,,,"; // stores written in deck file in variable.                     // writes contents of variable "deckwriting" in deck file.                     streamwriter writefile = file.appendtext(deckfile);                     writefile.writeline(deckwriting);                     writefile.close();                      validcheck = false;                 }             }             counter++;         }          file.close(); 

here have far, doesn't work. here trying do. if second section of first line in text file matches tempusernameout, nothing. if doesn't match, check next line. after checking lines, if second section of line doesn't match tempusernameout, write line stored in deckwriting end of text file. got base of code here. thanks!

https://msdn.microsoft.com/en-gb/library/aa287535(v=vs.71).aspx

at first, use "using" streams. way you'll sure stream closed if exception arised.

your issue in try write file when it's blocked reading stream. use bool variable checking whether need write file or not, , open stream writing when you've closed reading stream, this

var counter = 0; string line; bool validcheck = true;           // read file , display line line.         using (var file = new system.io.streamreader(deckfile))         {             while ((line = file.readline()) != null && validcheck == true)             {                 if (line.split(',')[1] == form1.tempusernameout)                 {                     validcheck = false;                     break;                 }                 counter++;             }         }          if (validcheck)         {             int linecountdecks = file.readalllines(deckfile).length + 1;             // makes variable set amount of lines in deck file + 1.             string deckwriting = linecountdecks.tostring() + "," + form1.tempusernameout +                                  ",1,,,,,,,,,,,2,,,,,,,,,,,3,,,,,,,,,,,4,,,,,,,,,,,5,,,,,,,,,,,";             // stores written in deck file in variable.             // writes contents of variable "deckwriting" in deck file.             using (var writefile = file.appendtext(deckfile))             {                 writefile.writeline(deckwriting);             }         } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -