FileSystem.OpenTextFieldParser Method

Definition

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

Overloads

OpenTextFieldParser(String, String[])

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

OpenTextFieldParser(String)

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

OpenTextFieldParser(String, Int32[])

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

OpenTextFieldParser(String, String[])

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

C#
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser(string file, params string[] delimiters);

Parameters

file
String

The file to be opened with the TextFieldParser.

delimiters
String[]

Delimiters for the fields.

Returns

TextFieldParser to read the specified file.

Exceptions

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \.\); it ends with a trailing slash.

file is Nothing.

The file does not exist.

The file is in use by another process, or an I/O error occurs.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

A row cannot be parsed using the specified format. The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.

The user lacks necessary permissions to view the path.

Examples

This example opens the TextFieldParser.reader and uses it to read from C:\TestFolder1\Test1.txt.

VB
Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
Dim currentRow As String()
While Not reader.EndOfData
    Try
        currentRow = reader.ReadFields()
        Dim currentField As String
        For Each currentField In currentRow
            MsgBox(currentField)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
    End Try
End While

Remarks

The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFieldParser method.

See also

Applies to

.NET 9 and other versions
Product Versions
.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

OpenTextFieldParser(String)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

C#
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser(string file);

Parameters

file
String

The file to be opened with the TextFieldParser.

Returns

TextFieldParser to read the specified file.

Exceptions

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \.\); it ends with a trailing slash.

file is Nothing.

The file does not exist.

The file is in use by another process, or an I/O error occurs.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

A row cannot be parsed using the specified format. The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.

The user lacks necessary permissions to view the path.

Examples

This example opens the TextFieldParser.reader and uses it to read from C:\TestFolder1\Test1.txt.

VB
Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
Dim currentRow As String()
While Not reader.EndOfData
    Try
        currentRow = reader.ReadFields()
        Dim currentField As String
        For Each currentField In currentRow
            MsgBox(currentField)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
    End Try
End While

Remarks

The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFieldParser method.

See also

Applies to

.NET 9 and other versions
Product Versions
.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

OpenTextFieldParser(String, Int32[])

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

The OpenTextFieldParser method allows you to create a TextFieldParser object, which provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldParser object can be used to read both delimited and fixed-width files.

C#
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser(string file, params int[] fieldWidths);

Parameters

file
String

The file to be opened with the TextFieldParser.

fieldWidths
Int32[]

Widths of the fields.

Returns

TextFieldParser to read the specified file.

Exceptions

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \.\); it ends with a trailing slash.

file is Nothing.

The file does not exist.

The file is in use by another process, or an I/O error occurs.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

A row cannot be parsed using the specified format. The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.

The user lacks necessary permissions to view the path.

Examples

This example opens the TextFieldParser.reader and uses it to read from C:\TestFolder1\Test1.txt.

VB
Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
Dim currentRow As String()
While Not reader.EndOfData
    Try
        currentRow = reader.ReadFields()
        Dim currentField As String
        For Each currentField In currentRow
            MsgBox(currentField)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
    End Try
End While

Remarks

The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFieldParser method.

See also

Applies to

.NET 9 and other versions
Product Versions
.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