Return StreamReader to Beginning

Solution 1:

You need to seek on the stream, like you did, then call DiscardBufferedData on the StreamReader. Documentation here:

Edit: Adding code example:

Stream s = new MemoryStream();
StreamReader sr = new StreamReader(s);
// later... after we read stuff
s.Position = 0;
sr.DiscardBufferedData();        // reader now reading from position 0

Solution 2:

I use this method:

System.IO.StreamReader reader = new System.IO.StreamReader("file.txt")
//end of reading
reader.DiscardBufferedData();
reader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin); 

Solution 3:

Amy's answer will work on some files but depending on the underlying stream's encoding, you may get unexpected results.

For example if the stream is UTF-8 and has a preamble, then the StreamReader will use this to detect the encoding and then switch off some internal flags that tells it to detect the encoding and check the preamble. If you reset the stream's position to the beginning, the stream reader will now consume the preamble again but it will include it in the output the second time. There is no public methods to reset this encoding and preamble state so the safest thing to do if you need to "rewind" a stream reader is to seek the underlying stream to the beginning (or set position) as shown and create a new StreamReader, just calling DiscardBufferedData() on the StreamReader will not be sufficient.