reset a streamreader to the beginning?

Posted on 2006-03-29
Last Modified: 2010-05-19

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?

Question by:riceman0
    LVL 85

    Accepted Solution

    by: this:

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

            line = sr.ReadLine
            While Not IsNothing(line)
                line = sr.ReadLine
            End While

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

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


    Author Comment


    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...)
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now