省略前5行Streamreader

时间:2015-01-07 11:18:26

标签: vb.net streamreader

我使用这个简单的代码来读取文本文件并将值返回到文本框

  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行并且只返回之后的内容。

请指点什么?

3 个答案:

答案 0 :(得分:3)

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

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))

因为,正如您所述,该文件始终"小"

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

使用File.ReadAllLines

答案 1 :(得分:1)

如果你想跳过前五行,只需阅读五次,但不要对它们做任何事情

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

答案 2 :(得分:1)

只计算您正在阅读的行数以及超过五行然后处理它

    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