FileSystemProxy.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.MyServices
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

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

Parameters

file
Type: System.String

The file to be opened with the TextFieldParser.

fieldWidths
Type: System.Int32[]

Widths of the fields.

Return Value

Type: Microsoft.VisualBasic.FileIO.TextFieldParser

TextFieldParser to read the specified file.

Exception Condition
ArgumentException

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.

ArgumentNullException

file is Nothing.

FileNotFoundException

The file does not exist.

IOException

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

PathTooLongException

The path exceeds the system-defined maximum length.

NotSupportedException

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

MalformedLineException

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.

SecurityException

The user lacks necessary permissions to view the path.

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

To

See

Read from a delimited text file

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

Read from a fixed-width text file

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

Read from a text file with multiple formats

How to: Read From Text Files with Multiple Formats in Visual Basic

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

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

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

SecurityPermission

Describes a set of security permissions applied to code. Associated enumeration: ControlEvidence.

.NET Framework
Available since 2.0
Return to top
Show: