Amy's answer will work with some files, but depending on the underlying streaming encoding, you may get unexpected results.
For example, if the stream is UTF-8 and has a preamble, then StreamReader will use this to detect the encoding, and then disable some internal flags that inform it of the detection of the encoding and verification of the preamble. If you reset the position of the stream to the beginning, the stream reader will now use the preamble again, but it will include it in the output a second time. There are no public methods for resetting this encoding state and preamble, so the safest action if you need to βrewindβ the stream reader is to look for the base stream at the beginning (or position), and create a new StreamReader, just calling DiscardBufferedData ( ) in StreamReader will be insufficient.
Eamon Jul 16 '15 at 2:04 2015-07-16 02:04
source share