Re-read stream

  • Thread starter Thread starter Morten.Snedker
  • Start date Start date
M

Morten.Snedker

How do I use a StreamReader more than once?

Dim rs As New StreamReader(fd.FileName, Encoding.Default)
Do While rs.Peek() >= 0
...
Loop

'Loop 2
Do While rs.Peek() >= 0
...
Loop

Now loop 2 doesn't work 'cause I've gotten to then end of the stream.
How do I "re-initialize", so I can perform another loop through the
stream?

Regards /Snedker
 
How do I use a StreamReader more than once?

Dim rs As New StreamReader(fd.FileName, Encoding.Default)
Do While rs.Peek() >= 0
...
Loop

'Loop 2
Do While rs.Peek() >= 0
...
Loop

Now loop 2 doesn't work 'cause I've gotten to then end of the stream.
How do I "re-initialize", so I can perform another loop through the
stream?

The simplest way would be to close the StreamReader and re-open it. You
*can* call DiscardBufferedData, and then set BaseStream.Position = 0 as
an alternative though.
 
Back
Top