Return StreamReader to the beginning

I am reading the file line by line and I want to restart reading by calling the Rewind() method.

How can I manipulate my System.IO.StreamReader and / or its underlying System.IO.FileStream to start by reading a file?

I have a smart idea to use FileStream.Seek(long, SeekOffset) to move around the file, but this does not affect the inclusion of System.IO.StreamReader . I could Close() and reassign both the stream and the referrals of the reader, but I hope that there will be a better way.

+58
c #
Jan 12 '10 at 23:19
source share
6 answers

You need to search the stream like you do, then type DiscardBufferedData on the StreamReader . The documentation is here :

Edit: adding sample code:

 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 
+97
Jan 12 '10 at 23:24
source share

I am using 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); 
+25
Aug 09 '15 at 20:50
source share

This is good if BaseStream can actually set the Position property to 0.

If you cannot (for example, the HttpWebResponse stream), then a good option would be to copy the stream to a MemoryStream ... there you can set Position to 0 and restart the Stream as you want.

+17
May 28 '10 at 15:22
source share

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.

+17
Jul 16 '15 at 2:04
source share
 public long ReadList(string fileName, Action<string> action,long position=0) { if (!File.Exists(fileName)) return 0; using (var reader = new StreamReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),System.Text.Encoding.Unicode)) { if (position > 0)reader.BaseStream.Position = position; while (!reader.EndOfStream) { action(reader.ReadLine()); } return reader.BaseStream.Position; } } 
+1
Jan 15 '16 at 18:36
source share

The question is looking for some StreamReader.Rewind() method. You are looking for StreamReader.BaseStream.Position = 0; which returns the reader to the beginning so that it can be read again.

 StreamReader sr = new StreamReader("H:/kate/rani.txt"); Console.WriteLine(sr.ReadToEnd()); sr.BaseStream.Position = 0; Console.WriteLine("----------------------------------"); while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } 
-one
Dec 08 '15 at 20:02
source share



All Articles