TextFieldParser.ReadLine Method

Definition

Returns the current line as a string and advances the cursor to the next line.

public string? ReadLine ();
public string ReadLine ();

Returns

The current line from the file or stream.

Examples

This example reads through the file ParserText.txt and writes it to 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

If Testfile.txt does not exist, it is created by the WriteAllText method.

This example writes the fields as a single string; to have each line display on its own line in the destination file, a VbCrLf character should be appended at the end of each line.

Remarks

The ReadLine method performs no parsing; an end-of-line character within a delimited field is interpreted as the actual end of the line.

Nothing is returned if the end of the file is reached.

Applies to

Product Versies
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also