134

My root problem is that when using calls Dispose on a StreamWriter, it also disposes the BaseStream (same problem with Close).

I have a workaround for this, but as you can see, it involves copying the stream. Is there any way to do this without copying the stream?

The purpose of this is to get the contents of a string (originally read from a database) into a stream, so the stream can be read by a third party component.
NB: I cannot change the third party component.

public System.IO.Stream CreateStream(string value)
{
    var baseStream = new System.IO.MemoryStream();
    var baseCopy = new System.IO.MemoryStream();
    using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
    {
        writer.Write(value);
        writer.Flush();
        baseStream.WriteTo(baseCopy); 
    }
    baseCopy.Seek(0, System.IO.SeekOrigin.Begin);
    return baseCopy;
}

Used as

public void Noddy()
{
    System.IO.Stream myStream = CreateStream("The contents of this string are unimportant");
    My3rdPartyComponent.ReadFromStream(myStream);
}

Ideally I'm looking for an imaginary method called BreakAssociationWithBaseStream, e.g.

public System.IO.Stream CreateStream_Alternate(string value)
{
    var baseStream = new System.IO.MemoryStream();
    using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
    {
        writer.Write(value);
        writer.Flush();
        writer.BreakAssociationWithBaseStream();
    }
    return baseStream;
}
3
  • This is a similar question: stackoverflow.com/questions/2620851 Apr 19, 2010 at 12:10
  • I was doing this with a stream from a WebRequest, interestingly, you can close it if the encoding is ASCII but not UTF8. Weird.
    – tofutim
    Mar 28, 2013 at 22:40
  • tofutim, I had mine encoded as ASCII, and it still disposes of the underlying stream.. Sep 17, 2014 at 13:20

5 Answers 5

138

If you are using .NET Framework 4.5 or later, there is a StreamWriter overload using which you can ask the base stream to be left open when the writer is closed.

In earlier versions of .NET Framework prior to 4.5, StreamWriter assumes it owns the stream. Options:

  • Don't dispose the StreamWriter; just flush it.
  • Create a stream wrapper which ignores calls to Close/Dispose but proxies everything else along. I have an implementation of that in MiscUtil, if you want to grab it from there.
10
  • 18
    Clearly the 4.5 overload was a not thought out concession -- the overload requires the buffer size, which cannot be 0 nor null. Internally I know that 128 characters is the minimum size, so I just set it to 1. Otherwise this 'feature' makes me happy. Sep 17, 2014 at 14:52
  • Is there a way to set that leaveOpen parameter after StreamWriter was created?
    – c00000fd
    Oct 19, 2018 at 5:16
  • @c00000fd: Not that I'm aware of.
    – Jon Skeet
    Oct 19, 2018 at 6:50
  • 1
    @Yepeekai: "if I pass a stream to a sub method and that sub method create the StreamWriter, it will be disposed at the end of the execution of that sub method" No, that's simply not true. It will only be disposed if something calls Dispose on it. The method ending doesn't do that automatically. It may be finalized later if it has a finalizer, but that's not the same thing - and it's still not clear what danger you're anticipating. If you think it's unsafe to return a StreamWriter from a method because it could be automatically disposed by the GC, that's just not true.
    – Jon Skeet
    Jan 29, 2019 at 16:14
  • 1
    @Yepeekai: And IIRC, StreamWriter doesn't have a finalizer - I wouldn't expect it to, for precisely this reason.
    – Jon Skeet
    Jan 29, 2019 at 16:15
49

.NET 4.5 has a new method for that:

StreamWriter(Stream, Encoding, Int32, Boolean)

public StreamWriter(
    Stream stream,
    Encoding encoding,
    int bufferSize,
    bool leaveOpen
)
3
  • Thanks mate! Did not know this, and if anything that would be a fine reason for me to start targeting .NET 4.5! Jan 15, 2014 at 13:54
  • 26
    Shame there is no overload that doesn't require bufferSize to be set. I'm happy with the default there. I'm having to pass it myself. Not the end of the world. Feb 27, 2014 at 14:57
  • 6
    The default bufferSize is 1024. Details are here.
    – Alex Klaus
    Mar 16, 2018 at 4:29
37

Simply don't call Dispose on the StreamWriter. The reason this class is disposable is not because it holds unmanaged resource but to allow the disposal of the stream which itself could hold unmanaged resources. If the life of the underlying stream is handled elsewhere, no need to dispose the writer.

9
  • 2
    @Marc, wouldn't calling Flush do the job in case it buffers data? Apr 19, 2010 at 11:26
  • 3
    Fine, but once we exit CreateStream, the StreamWrtier is collectable, forcing the third part reader to race against the GC, which is not a situation I want to be left in. Or am I missing something? Apr 19, 2010 at 11:33
  • 9
    @BinaryWorrier: No, there's no race condition: StreamWriter doesn't have a finalizer (and indeed shouldn't).
    – Jon Skeet
    Apr 19, 2010 at 12:15
  • 10
    @Binary Worrier: You should only have a finalizer if you directly own the resource. In this case, the StreamWriter should assume that the Stream will clean itself up if it needs to.
    – Jon Skeet
    Apr 19, 2010 at 14:34
  • 2
    It seems that the 'close' method of StreamWriter also closes and disposes of the stream. So one must flush, but not close or dispose the streamwriter, so it doesn't close the stream, which would do the equivalent of dispose the stream. Way too much "help" from the API here. Sep 17, 2014 at 13:41
8

Memory stream has a ToArray property that can be used even when stream is closed. To Array writes the stream contents to a byte array, regardless of the Position property. You can create a new stream based on the stream you wrote in.

public System.IO.Stream CreateStream(string value)
{
    var baseStream = new System.IO.MemoryStream();
    var baseCopy = new System.IO.MemoryStream();
    using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
    {
        writer.Write(value);
        writer.Flush();
        baseStream.WriteTo(baseCopy); 
    }
    var returnStream = new System.IO.MemoryStream( baseCopy.ToArray());
    return returnStream;
}
1
  • Does that correctly limit the returned array to the content size? Because Stream.Position can not be called after it's disposed.
    – Nyerguds
    Mar 15, 2016 at 11:56
2

You need to create a descendant of the StreamWriter and override its dispose method, by always passing false to the disposing parameter, it will force the stream writer NOT to close, the StreamWriter just calls dispose in the close method, so there is no need to override it (of course you can add all the constructors if you want, i just have one):

public class NoCloseStreamWriter : StreamWriter
{
    public NoCloseStreamWriter(Stream stream, Encoding encoding)
        : base(stream, encoding)
    {
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(false);
    }
}
6
  • 3
    I believe this does not do what you think it does. The disposing flag is part of the IDisposable pattern. Always passing false to the Dispose(bool) method of the base class basically signals to the StreamWriter that it is being called from the finalizer (which is not so when you call Dispose() explicitly), and should thus not access any managed objects. This is why it won't dispose the base stream. However, the way you've achieved this is a hack; it would be much simpler to simply not call Dispose in the first place! Aug 11, 2012 at 14:59
  • Thats Symantec's really, anything you do apart from rewriting the streaming entirely from scratch is going to be a hack. Definitely though, you could simply not call base.Dispose(false) at all, but there would be no functional difference, and I like the clarity of my example. However, keep this in mind, a future version of the StreamWriter class may do more than just close the stream when it disposes, so calling dispose(false) future proofs it as well. But to each his own. Aug 12, 2012 at 11:59
  • 2
    Another way to do it would be to create your own stream wrapper which contains another stream where the Close method simply does nothing instead of closing the underlying stream, this is less of a hack but is a more work. Aug 12, 2012 at 12:03
  • Amazing timing: I was just about to suggest the same thing (decorator class, possibly named OwnedStream, that ignores Dispose(bool) and Close). Aug 12, 2012 at 12:04
  • Yeah, the code above is how I do it for a quick and dirty method, but if I was making a commercial application or something that actually mattered to me I would do it correctly using the Stream wrapper class. Personally I think Microsoft made a mistake here, the streamwriter should have had a boolean property to close the underlying stream instead, but then I dont work at Microsoft so they do what they like I guess :D Aug 12, 2012 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.