Question Need to reset a StreamReader or Make a new instance of StreamReader

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
How can I reset a StreamReader that return to beginning(start from beginning)?
I almost solve my issue that the reason only a StreamReader that it must be start from beginning when I get a line of string done. I want the same stance instance of treamReader to read continually. If not I must make more instance of StreamReader for each Do Until loop to get right line to assign.

 curline = seclist.Count
        Dim reachToLine As Integer = 0
        Dim swaptxt As String = ""
        '--this part is for only an example--
        Do Until curline = 0
            Select Case curline
                Case 5
                    Do Until reachToLine = seclist.Item(0)
                        swaptxt = sr2.ReadLine
                        reachToLine += 1
                    Loop
                    Label1.Text = swaptxt
                    reachToLine = 0
                Case 4
                    Do Until reachToLine = seclist.Item(1)
                        swaptxt = sr2.ReadLine
                        reachToLine += 1
                    Loop
                    Label2.Text = swaptxt
                    reachToLine = 0
                Case 3
                    Do Until reachToLine = seclist.Item(2)
                        swaptxt = sr2.ReadLine
                        reachToLine += 1
                    Loop
                    Label3.Text = swaptxt
                    reachToLine = 0
                Case 2
                    Do Until reachToLine = seclist.Item(3)
                        swaptxt = sr2.ReadLine
                        reachToLine += 1
                    Loop
                    Label4.Text = swaptxt
                    reachToLine = 0
                Case 1
                    Do Until reachToLine = seclist.Item(4)
                        swaptxt = sr2.ReadLine
                        reachToLine += 1
                    Loop
                    Label5.Text = swaptxt
                    reachToLine = 0
                Case Else
                    MessageBox.Show("list disi")
            End Select
            curline -= 1
        Loop
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

If you want to continue with the way you are doing things and reset a StreamReader to start reading from the beginning of the file again then you need to Discard any Buffered Data first and then reset the file pointer of the Stream back to the start of the file. i.e:-

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Using myReader As New StreamReader("d:\temp\html.txt")
    Dim someData As String = myReader.ReadLine
    MsgBox(someData)
 
    'Reset the StreamReader to start at the beginning of the file
    myReader.DiscardBufferedData()
    myReader.BaseStream.Position = 0
    someData = myReader.ReadLine
    MsgBox(someData)
  End Using
End Sub


Also, have a read of this to find out why it is important to discard any buffered data first:-

StreamReader.DiscardBufferedData Method (System.IO)

An alternative is to just read the whole file into a List(Of String) first and then just keep reusing the same list.

Hope that helps.

Cheers,

Ian
 

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
Below is also nice to solve the matter.
StreamReader. BaseStream.Seek(0, SeekOrigin.Begin)


Thanks for rapid useful informing.
---Btw, I just tried the code above here that caused a matter and I also tried yours that your is working excellent!!, you are right in your solution. Mine is that needs your code as myReader.DiscardBufferedData()
 
Last edited:
Top Bottom