Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

TextFieldParser.ReadFields Method

Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public string[] ReadFields()

Return Value

Type: System.String[]
An array of strings that contains field values for the current line.
ExceptionCondition
MalformedLineException

A field cannot be parsed by using the specified format.

In order to allow users to parse text files in multiple formats, the ReadFields method examines the values of TextFieldType, Delimiters, and FieldWidths, if they are specified, each time it is called. Users need to correctly configure the TextFieldType and FieldWidths or Delimiters properties, as appropriate. If TextFieldType is set to Delimited, and Delimiters is not set, or if TextFieldType is set to FixedWidth and FieldWidths, an exception is thrown.

If ReadFields encounters blank lines, they are skipped and the next non-blank line is returned.

If the ReadFields method cannot parse the current line, it raises an exception and does not move to the next line. This enables your application to attempt to parse the line again.

The following table lists examples of tasks involving the ReadFields method.

This example uses the ReadFields method to read from the comma-delimited file ParserText.txt. The example writes the fields 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.ReadFields()
            For Each currentField As String In currentRow
                My.Computer.FileSystem.WriteAllText(
                    "C://testfile.txt", currentField, True)
            Next 
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try 
    End While 
End Using

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.