I want to read a specific line from an html source code.
Im storing the source into a string file and i want to read the line X.
So im using this method that i found on net
Public Shared Function ReadSpecifiedLine(file As String, lineNum As Integer) As String
Dim contents As String = String.Empty
Try
Using stream As New StreamReader(file)
contents = stream.ReadToEnd()
Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})
If linesArray.Length > 1 Then
If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
Return linesArray(lineNum)
Else
Return linesArray(0)
End If
Else
Return contents
End If
End Using
Catch ex As Exception
Return ex.ToString()
End Try
End Function
For example im trying to read the 4th line and im getting this error.
System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.IO.Path.GetFileName(String path)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamReader..ctor(String path)
at WindowsApplication1.Form1.ReadSpecifiedLine(String file, Int32 lineNum) in C:\Users\Optimus\documents\visual studio 2012\Projects\WindowsApplication1\WindowsApplication1\Form1.vb:line 48
Any help would be appreciated.
The method you posted assumes you are passing in the file path. If you want to change it to accept the actual file contents, instead of the file path, you could simplify the method by getting rid of the stream object: