VB逐行读取,比较一个值并提取部分以显示

时间:2016-04-22 21:30:41

标签: vb.net streamreader

我在驱动器C上有一个文本文件,我需要打开并读取第一行并执行代码显示,然后读取第二行并执行相同操作等等,直到它读取文本中的所有行文件。 不知道怎么做......

我的代码

我想我想出来了,下面,但我该怎么做呢?对于文本文件的每一行,意思是,每行应用相同的评估?

    'type of message triggers
    Dim type1 As String = "d1err./"
    Dim type2 As String = "d1jam./"
    Dim type3 As String = "d1spe./"
    Dim type4 As String = "d2err./"
    Dim type5 As String = "d2jam./"
    Dim type6 As String = "d2spe./"

    Dim fileReader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\A\data\display.txt")
    Dim stringReader As String = fileReader.ReadLine()

    While stringReader <> Nothing
        Dim LineStart As String = stringReader.Substring(0, 7)
        Dim RestOfLine As String = stringReader.Substring(7)

        If LineStart = type1 Then
            Display1.Dis1RTB1.Clear()
            Display1.Dis1RTB1.Text = RestOfLine
        ElseIf LineStart = type2 Then
            Display1.Dis1RTB2.Clear()
            Display1.Dis1RTB2.Text = RestOfLine
        ElseIf LineStart = type3 Then
            Display1.Dis1RTB3.Clear()
            Display1.Dis1RTB3.Text = RestOfLine
        ElseIf LineStart = type4 Then
            Display2.Dis2RTB1.Clear()
            Display2.Dis2RTB1.Text = RestOfLine
        ElseIf LineStart = type5 Then
            Display2.Dis2RTB2.Clear()
            Display2.Dis2RTB2.Text = RestOfLine
        ElseIf LineStart = type6 Then
            Display2.Dis2RTB3.Clear()
            Display2.Dis2RTB3.Text = RestOfLine
        End If
        stringReader = fileReader.ReadLine()
    End While
    fileReader.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

也许这会对你有所帮助

Public Class Form1
Dim fileReader As New IO.StreamReader("C:\Users\User\Desktop\New Text Document.txt")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Not fileReader.EndOfStream Then
        TextBox1.Text += fileReader.ReadLine & vbNewLine
    End If

    'Or you could do it like this

    While Not fileReader.EndOfStream
        Dim tmpStr As String = fileReader.ReadLine
        Select Case tmpStr
            Case "Dis1err"
                'Do Work here
        End Select
    End While
End Sub

结束班