954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

IO.StreamReader Help?

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?

RedexProGamma
Newbie Poster
23 posts since Sep 2011
Reputation Points: 10
Solved Threads: 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

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

Thank you very much Teme!

RedexProGamma
Newbie Poster
23 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: