[Solved] Where to use StreamReader.DiscardBufferedData()? [closed]


From MSDN:

Use the DiscardBufferedData method to reset the internal buffer for the StreamReader object. You need to call this method only when the position of the internal buffer and the BaseStream do not match.

So a little example:

Stream s = new MemoryStream();
StreamReader sr = new StreamReader(s);
// Read things here.
s.Position = 10;
sr.DiscardBufferedData();        // reader now reading from position 0
sr.BaseStream.Seek(10, SeekOrigin.Begin); // Reader now reading from location 10.

In your case:

StreamReader.BaseStream.Position = SOMETHING_ELSE;
sr.DiscardBufferedData();
StreamReader.BaseStream.Seek(SOMETHING_ELSE, SeekOrigin.Begin);
Console.WriteLine(StreamReader.ReadLine());

and dont forget to set back again at:

StreamReader.BaseStream.Position = CursorValue;
sr.DiscardBufferedData();
StreamReader.BaseStream.Seek(CursorValue, SeekOrigin.Begin);

solved Where to use StreamReader.DiscardBufferedData()? [closed]