reset a streamreader to the beginning?


Hey, I am using a System.IO.StreamReader, specifically the ReadLine method to parse through an ascii file and extract the data I need.  I am using this because I used to use the old line input command a lot (actually have used it for about 15 years so this a weird shift).  Anyway, the ReadLine method, called successively, reads successive lines of the file.  Is there a way to reset this to the beginning, so that the next ReadLine method reads the first line, without closing and reinstantiating the reader?

Thanks.
riceman0Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes...like this:

        Dim sr As New System.IO.StreamReader("c:\somefile.txt")
        Dim line As String

        Debug.WriteLine("Contents:")
        line = sr.ReadLine
        While Not IsNothing(line)
            Debug.WriteLine(line)
            line = sr.ReadLine
        End While

        sr.DiscardBufferedData()
        sr.BaseStream.Seek(0, IO.SeekOrigin.Begin)

        Debug.WriteLine("Contents Again:")
        line = sr.ReadLine
        While Not IsNothing(line)
            Debug.WriteLine(line)
            line = sr.ReadLine
        End While

        sr.Close()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
riceman0Author Commented:

Thanks.  Question: I guess that means that the StreamReader contains a BaseStream object, rather than derives from the BaseStream class (in which case the basestream methods would just appear as streamreader methods, right?).  Any idea why they did it that way?  (As you might remember I'm just getting the hang of .NET framework...)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's a good question...

I think they did it that way so they could control how the underlying stream is accessed via a StreamReader.  This makes it ~easier~ to use for the end user because it is harder to screw up the way the file is read since you only have a limited way in which you can access the file stream.

They provided other classes to access file streams in different ways or at lower levels.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.