[C#] My StreamWriter isn’t Writing Properly!

I have a StreamWriter which is responsible for writing out to my test file, however, there is a chance that my test app may be killed before completed. Whenever this occurred, it seemed that my test file was blank! Even though I knew that I had written some data to the stream…

Turns out I misunderstood exactly how the StreamWriter worked. The data is actually written into a buffer until you explicitly call Flash() or Close(). Because my Close() call was never being called when the app was killed, this data was never actually being flushed from the buffer into the file! Doh!

_resultStreamWriter.WriteLine("Write me now!!!");
_resultStreamWriter.Flush();

MSDN – StreamWriter.Flush Method



Tags:
This entry was posted on Tuesday, May 26th, 2009 at 1:37 pm and is filed under c#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Your comment