打开文本文件并逐行读取

时间:2019-04-25 06:35:19

标签: vb.net vb6

我有此代码,我需要将其从vb6转换为vb.net,但我无法理解。

 Open textFile For Input As #1
    Do While Not EOF(1) 
        Line Input #1, textline
        If Left(textline, 5) = "56,1," Then
            kraj = 1
            If Left(Right(textline, 4), 1) = "+" Then
                Open textFile2 For Output As #2 
                Print #2, "53,1,______,_,__;"
                 Print #2, "56,1,______,_,__;"
             Close #2
                Close #1
                Exit Sub
            Else
                a = InStr(textline, ";")
                cilj = Right(textline, Len(textline) - a)
                a1 = InStr(cilj, ",")
                cilj1 = Right(cilj, Len(cilj) - a1)
                a2 = InStr(cilj1, ",")
                If bfr <> 0 Then
                    cilj2 = Right(cilj1, Len(cilj1) - a2)
                    broj = Left(cilj2, Len(cilj2) - 1)
                Else
                    broj = Left(cilj1, a2)
                End If
                If IsNumeric(broj) = True Then
                    broj = CDbl(broj)
                    Close #1
                    Exit Sub
                Else
                    MsgBox "Error", vbCritical, "Error"
               Close #1
                    Exit Sub
                End If
            End If
        End If
    Loop

我如何将其转换为VB.NET,我使用相同的方法还是需要将行放入数组中。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:3)

您可以使用Reader对象,并将它们与该对象一起使用,可以逐行读取。
示例:

Dim reader = File.OpenText(filetoimport.Text)

    Dim line As String = Nothing

    While (reader.Peek() <> -1)
        line = reader.ReadLine()
        // Your code
    End While