0

Hey all (who see this),
I am currently writing a Login Script, and I am having trouble making StreamReader read a specific line in a text file.
This is what I have so far (not the entire script, just the reader lines):

Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        Dim file As String = (path + "\LogDat.txt")
        Dim sr As New IO.StreamReader(file)
        Dim line1 As String = sr.ReadLine(1) '' Supposed to read line 1
        Dim line4 As String = sr.ReadLine(4) '' Supposed to read line 4
        Dim line9 As String = sr.ReadLine(9) '' Supposed to read line 9

Any ideas on what I am doing wrong?

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by RedexProGamma
0

There's not such a method in StreamReader class that reads a specific line, see StreamReader.ReadLine Method. Instead, if you need a specific line, read the lines to a string array and get the specific line from the array.

Another solution would be to use counter variable for the lines:

Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim file As String = (path + "\LogDat.txt")
Dim sr As New IO.StreamReader(file)
Dim line1 As String
Dim line4 As String
Dim line9 As String
Dim thisLine As String
Dim lineCounter As Integer = 1

Do
    thisLine = sr.ReadLine()
    If thisLine IsNot Nothing Then
        If lineCounter = 1 Then line1 = thisLine
        If lineCounter = 4 Then line4 = thisLine
        If lineCounter = 9 Then line9 = thisLine
    End If
    lineCounter += 1
Loop Until thisLine Is Nothing
sr.Close()

HTH

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.