Search code examples
c#winforms.net-2.0streamwriter

WriteLine into the text file and changes preview


Can I write line of text into file and see changes after each call WriteLine? Now I see changes in this file only after closing StreamWriter.

StreamWriter sw = new StreamWriter(string path);
sw.WriteLine("text");

in this place I want to preview my file

sw.Close();

now I see writed line only in this moment


Solution

  • Output to the StreamWriter is buffered by default. Try this:

    sw.AutoFlush = true;
    

    See here http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush.aspx