OpenTextFieldParser Method (String, Int32[])

FileSystem.OpenTextFieldParser Method (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.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static TextFieldParser OpenTextFieldParser(
	string file,
	params int[] fieldWidths


Type: System.String
The file to be opened with the TextFieldParser.
Type: System.Int32[]
Widths of the fields.

Return Value

Type: Microsoft.VisualBasic.FileIO.TextFieldParser
TextFieldParser to read the specified file.


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.

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

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

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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2015 Microsoft