StreamReader - reset to the top

samshiell

Member
Joined
Oct 26, 2004
Messages
10
Programming Experience
10+
Hi

When using StreamReader how can you reset the "pointer" to the beginning?

I'm reading some lines then, when a certain condition is met I want to read the whole file.

I've tried setting the BaseStream.Position = 0 but that acheives nothing.

This is my (extremely simplified) code....

fReadIn = New StreamReader(strFileName)
strLine = fReadIn.Readline
strLine = fReadIn.Readline
''.... various stuff and conditions happen here
fReadIn.BaseStream.Position = 0
strFileContents = fR.ReadToEnd

Any ideas?

Thanks

Sam
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
fReadIn.Close
fReadIn = new StreamReader(strFileName)

will close the read, then re-open it (which puts it back at the beginning of the file)
 

jlundberg

New member
Joined
Jun 3, 2009
Messages
1
Programming Experience
5-10
fReadIn = New StreamReader(strFileName)
strLine = fReadIn.Readline
strLine = fReadIn.Readline
''.... various stuff and conditions happen here
fReadIn.BaseStream.Position = 0
strFileContents = fR.ReadToEnd

fReadIn.BaseStream.Seek(0, SeekOrigin.Begin) 'will return the reader to the begining
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
fReadIn.BaseStream.Seek(0, SeekOrigin.Begin) 'will return the reader to the begining
"Seek(0, SeekOrigin.Begin)" is equivalent to "Position = 0", neither will work as is because the StreamReader uses an internal buffer and read position. This will work however if you pair it with a call to DiscardBufferedData method to reset the StreamReader buffers.
 

samshiell

Member
Joined
Oct 26, 2004
Messages
10
Programming Experience
10+
Good grief, I posted this original question 5 years ago (October 2004) and have absolutely no idea now of what I was trying to achieve, why I was doing or what I did in the end.

Thanks anyway for your contribution

Regards

Sam
 
Top Bottom