我只需要从文本文件中读取行并显示它们。当我运行这个时,我可以看到id做了我想要的,但是在它读取最后一个值后,它只是在我的屏幕上显示一个空白表单而不继续前进。似乎无法找到文件的末尾或其他内容。我没有收到错误。
Using sr As New System.IO.StreamReader(Application.StartupPath & "\myfile.cfg")
Dim Line As String = ""
Dim i As Integer = 0
Dim temp_array As Array
Do While Line IsNot Nothing
Line = sr.ReadLine
temp_array = Line.Split("=")
'MessageBox.Show(temp_array(0))
Loop
End Using
答案 0 :(得分:3)
这是错误的代码,因为您在测试它是Line
之前是否真的要使用Nothing
。以下是循环文本文件行的两个很好的选项:
Using reader As New StreamReader(filePath)
Dim line As String
Do Until reader.EndOfStream
line = reader.ReadLine()
'...
Loop
End Using
For Each line In File.ReadLines(filePath)
'...
Next
正如您所看到的,第二个更简洁,但确实需要.NET 4.0或更高版本。