TextFieldParser.ReadLine 方法

更新:2007 年 11 月

以字符串形式返回当前行,并将光标向前移到下一行。

' Usage
Dim value As String = TextFieldParserObject.ReadLine()
' Declaration
Public Function ReadLine() As String

返回值

String

异常

下面的情况可能会导致引发异常:

备注

ReadLine 方法不执行分析;分隔字段内中的行尾字符将被解释为实际的行尾。

如果到达文件的结尾,则会返回 Nothing。

示例

此示例将从头到尾读取文件 ParserText.txt,并将其写入 Testfile.txt。

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {","}
    Dim currentRow As String
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadLine()
            My.Computer.FileSystem.WriteAllText _
           ("C://testfile.txt", currentRow, True)
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try
    End While
End Using

如果 Testfile.txt 不存在,WriteAllText 方法将会创建它。

此示例以单一字符串的形式写入字段;为了让每一行显示在目标文件中它自己的行上,应在每一行的结尾追加一个 VbCrLf 字符。

要求

命名空间:Microsoft.VisualBasic.FileIO

类:TextFieldParser

**程序集:**Visual Basic 运行库(位于 Microsoft.VisualBasic.dll 中)

权限

不需要任何权限。

请参见

概念

使用 TextFieldParser 对象分析文本文件

参考

TextFieldParser 对象

TextFieldParser.ReadLine

TextFieldParser.ReadFields 方法

TextFieldParser.ReadToEnd 方法