This is an old one, but I wanted to do something similar today and found that things have changed. Since .net 4.5, there is a leaveOpen
argument:
public StreamReader( Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen )
The only problem is that it is not entirely obvious what to set for the other parameters. Here is some help:
From the msdn page for StreamReader Constructor (Stream):
This constructor initializes the encoding to UTF8Encoding, the
BaseStream property using the stream parameter, and the internal
buffer size to 1024 bytes.
That just leaves detectEncodingFromByteOrderMarks
which judging by the source code is true
public StreamReader(Stream stream)
: this(stream, true) {
}
public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
: this(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks, DefaultBufferSize) {
}
It would be nice if some of those defaults were exposed or if the arguments were optional so that we could just specify the ones that we want.