Here is a piece of code -

Console.WriteLine(s); a = Console.ReadLine(); FileInfo f = new FileInfo(@"C:\kardlnal\i.txt"); StreamWriter w = f.CreateText(); w.WriteLine(a); w.Close(); 

Everything seems to work, but there is one thing. This action is enclosed in a loop; after each launch, the variable s changes values ​​and, after each launch, the file C:\kardlnal\i.txt what was written before. I also need this team to finish writing, and not rewrite it all over again.
Is it possible to do so or not?

    2 answers 2

    First of all, it makes no sense to create FileInfo .

    StreamWriter second parameter takes a append , which can be set to true and then the values ​​will be added to the file.

     using (StreamWriter w = new StreamWriter(@"C:\kardlnal\i.txt", true)) { w.WriteLine(a); } 

    Thirdly, if everything is recorded in your cycle, then why do you need to re-create a StreamWriter at each iteration? Why not create it before the cycle and close after?

      Of course available.
      For example:

       using (var stream = new FileStream(@"C:\kardlnal\i.txt", FileMode.Append)) using (StreamWriter w = new StreamWriter(stream)) { w.WriteLine(a); } 

      Here, the second argument of the FileStream constructor is the file open mode, FileMode.Append .