vb.net 获取文本文件一行的内容的多种方法,每行后面注释
时间: 2024-03-05 18:48:10 浏览: 98
1. 使用StreamReader类读取文件并逐行读取内容
```vb.net
Dim filePath As String = "C:\example.txt"
Using sr As New StreamReader(filePath)
While Not sr.EndOfStream
Dim line As String = sr.ReadLine()
'处理读取到的每一行内容
End While
End Using
```
2. 使用File类读取文件并逐行读取内容
```vb.net
Dim filePath As String = "C:\example.txt"
Dim lines As String() = File.ReadAllLines(filePath)
For Each line As String In lines
'处理读取到的每一行内容
Next
```
3. 使用TextFieldParser类读取文件并逐行读取内容
```vb.net
Dim filePath As String = "C:\example.txt"
Using parser As New TextFieldParser(filePath)
parser.TextFieldType = FieldType.Delimited '指定字段类型为分隔符
parser.SetDelimiters(",") '指定分隔符为逗号
While Not parser.EndOfData
Dim fields As String() = parser.ReadFields()
Dim line As String = String.Join(",", fields)
'处理读取到的每一行内容
End While
End Using
```
以上三种方法均可以逐行读取文本文件的内容,具体使用哪种方法取决于读取文件的需求和文件的格式。
阅读全文