Read specific line with StreamReader

I'm using the attached code to read the first line of a text file using StreamReader.

How do I read specific lines? I only want the third line in this instance. (Peek?)
Dim Str As System.IO.Stream
        Dim srRead As System.IO.StreamReader

            Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("http://Somewebsite/file.txt")
            Dim resp As System.Net.WebResponse = req.GetResponse
            Str = resp.GetResponseStream
            srRead = New System.IO.StreamReader(Str)

            Return srRead.ReadLine

            srRead.Close()
            Str.Close()

Open in new window

LVL 1
madlanAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
To get the third line using your code, just call ReadLine() twice before you actually capture the important line:
Dim Str As System.IO.Stream
Dim srRead As System.IO.StreamReader

Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("http://Somewebsite/file.txt")
Dim resp As System.Net.WebResponse = req.GetResponse
Str = resp.GetResponseStream
srRead = New System.IO.StreamReader(Str)

srRead.ReadLine
srRead.ReadLine

Return srRead.ReadLine

srRead.Close()
Str.Close()

Open in new window

0
 
lazyberezovskyCommented:
No indexing is available.
Use ReadLine if you need to read specific lines.

E.g:
Const lineIndexToRead As Integer = 1
Dim line As String
Dim lineIndex As Integer = 0
While (line = srRead.ReadLine()) <> Nothing
	If lineIndex = lineIndexToRead Then
		'process line here
	End If

	lineIndex = lineIndex + 1
End While

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.