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?

Recommended Answers

All 2 Replies

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

Thank you very much Teme!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.