共用方式為


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 字元附加在每一行的結尾。

需求

命名空間 (Namespace)︰Microsoft.VisualBasic.FileIO

類別︰TextFieldParser

組件:Visual Basic 執行階段程式庫 (在 Microsoft.VisualBasic.dll 中)

使用權限

不需要使用權限。

請參閱

概念

使用 TextFieldParser 物件剖析文字檔

參考

TextFieldParser 物件

TextFieldParser.ReadLine

TextFieldParser.ReadFields 方法

TextFieldParser.ReadToEnd 方法