Search code examples
vb.netstreamreader

How do you select the specific line to read in vb.net?


I was wondering if/how you can read a specific line in vb.net using a system.io.streamreader.

Dim streamreader as system.io.streamreader
streamreader.selectline(linenumber as int).read
streamreader.close()

Is this possible or is there a similiar function to this one?


Solution

  • I'd use File.ReadAllLines to read in the lines into an array, then just use the array to select the line.

    Dim allLines As String() = File.ReadAllLines(filePath)
    Dim lineTwo As String = allLines(1) '0-based index
    

    Note that ReadAllLines will read the entire text file into memory but I assume this isn't a problem because, if it is, then I suggest you take an alternative approach to trying to jump to a specific line.