Search code examples
c#eventsfile-iofilestreamusing

Write to file with FileStream without using "using"


I have a class member variable _stream which is a FileStream. This variable gets initialized in the constructor and needs to stick around until I'm done with the class. I have another class that raises an event every time something needs to be written to the file. In my main class I have an event handler that performs the actual writing. I tried the following code as in this question:

void MyEventHandler(string message)
{
    using (_stream)
        using (StreamWriter s = new StreamWriter(_stream))
           s.WriteLine(message);
}

But this doesn't work because using disposes of my FileStream before I'm done with it. What should I be doing instead?


Solution

  • Firstly, you clearly need to remove the using (_stream) as that will dispose of the stream directly.

    Secondly, the StreamWriter will also dispose of the stream as well, with your current code. Options for this are:

    • Don't have a using statement for the StreamWriter either, but explicitly flush the writer if you need it to be written to the file immediately:

      void MyEventHandler(string message)
      {
          var s = new StreamWriter(_stream);
          s.WriteLine(message);
          s.Flush();
      }
      
    • Use the constructor for StreamWriter which allows you to control whether or not the stream is closed when the StreamWriter is disposed:

      void MyEventHandler(string message)
      {
          using (var s = new StreamWriter(_stream, Encoding.UTF8, 1024, true))
          {
               s.WriteLine(message);
          }
      }
      
    • Change your code so that instead of a Stream, the field in your class is a TextWriter to start with (which is initialized as a StreamWriter for whatever file/stream you want) - then you don't need to create a new StreamWriter in the method at all.

    As Servy notes, your own class should implement IDisposable and dispose of the stream/writer in its Dispose method.