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.

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

Return Value

String ().

Exceptions

The following condition may cause an exception to be thrown:

Remarks

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.

Tasks

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

To See

Read from a delimited file

How to: Read From Comma-Delimited Text Files in Visual Basic

Read from a fixed-width file

How to: Read From Fixed-width Text Files in Visual Basic

Example

This example uses the ReadFields method to read from the file ParserText.txt and write 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

Requirements

Namespace: Microsoft.VisualBasic.FileIO

Class: TextFieldParser

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Permissions

The following permissions are required:

Permission Description

FileIOPermission

Associated enumeration: Unrestricted.

SecurityPermission

Associated enumeration: ControlEvidence.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Read From Comma-Delimited Text Files in Visual Basic
How to: Read From Fixed-width Text Files in Visual Basic
How to: Read From Text Files with Multiple Formats in Visual Basic

Reference

TextFieldParser Object
TextFieldParser.TextFieldType Property
TextFieldParser.FieldWidths Property
TextFieldParser.Delimiters Property
TextFieldParser.ReadLine Method
Microsoft.VisualBasic.FileIO.TextFieldParser.ReadFields

Concepts

Parsing Text Files with the TextFieldParser Object