省略前 5 行 Streamreader

Omitting first 5 lines Streamreader

我正在使用这个简单的代码来读取文本文件和return文本框的值

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

在我的文本文件的顶部将始终有 5 行以类似 name= 的内容开头。我想绕过前 5 行,只绕过 return 之后的内容。

有什么指点吗?

您可以这样做,但我建议您采用更好的方法,请参见下文。

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

或更好,

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

因为,如您所述,文件 始终 "small"

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

使用File.ReadAllLines

如果您想跳过前五行,只需阅读五遍即可,但不要对它们进行任何操作

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

只需数一下您正在阅读的行数,当超过五行时再进行处理

    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