elmonko
elmonko

Reputation: 685

Omitting first 5 lines Streamreader

I'm using this simple code to read a textfile and return the values to a text box

  Dim xread As StreamReader
  xread = File.OpenText(AllDetails(n).uPath & ".txt")
  Do Until xread.EndOfStream
      lstEmail.Text = lstEmail.Text & xread.ReadLine & vbCrLf
  Loop

At the top of my text file will always be 5 lines that start something like name=. I want to bypass the first 5 lines and only return what's after that.

Any pointers please?

Upvotes: 2

Views: 1923

Answers (3)

Jodrell
Jodrell

Reputation: 35716

You could just do this but I'd suggest a better approach, see below.

Dim xread = File.OpenText(AllDetails(n).uPath & ".txt")

xread.ReadLine()
xread.ReadLine()
xread.ReadLine()
xread.ReadLine()
xread.ReadLine()

Do Until xread.EndOfStream
      lstEmail.Text = lstEmail.Text & xread.ReadLine() & vbCrLf
Loop

or better,

lstEmail.Text = String.Join( _
   vbCrlf, _
   File.ReadLines(AllDetails(n).uPath & ".txt").Skip(5))

since, as you state, the file is always "small"

lstEmail.Text = String.Join( _
   vbCrlf, _
   File.ReadAllLines(AllDetails(n).uPath & ".txt").Skip(5))

use File.ReadAllLines

Upvotes: 4

Matt Wilko
Matt Wilko

Reputation: 27322

Just count the number of lines you are reading and when over five then process it

    Dim xread As StreamReader
    Dim lineNo As Integer = 1
    xread = File.OpenText(AllDetails(n).uPath & ".txt")
    Do Until xread.EndOfStream
        If lineNo > 5 Then
            lstEmail.Text = lstEmail.Text & xread.ReadLine & vbCrLf
        Else
            lineNo += 1
        End If
    Loop

Upvotes: 1

James Hunt
James Hunt

Reputation: 2528

If you want to skip the first five lines, just read them five times but don't do anything with them

Dim xread As StreamReader
xread = File.OpenText(AllDetails(n).uPath & ".txt")
For a = 1 To 5
    xread.ReadLine()
Next
Do Until xread.EndOfStream
    lstEmail.Text = lstEmail.Text & xread.ReadLine & vbCrLf
Loop

Upvotes: 1

Related Questions