Redigera

Dela via


FileSystemProxy.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, 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[])

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.

public:
 Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file, ... cli::array <int> ^ fieldWidths);
public Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser (string file, params int[] fieldWidths);
member this.OpenTextFieldParser : string * int[] -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Function OpenTextFieldParser (file As String, ParamArray fieldWidths As Integer()) As TextFieldParser

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.

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.

To See
Read from a delimited text file How to: Read From Comma-Delimited Text Files
Read from a fixed-width text file How to: Read From Fixed-width Text Files
Read from a text file with multiple formats How to: Read From Text Files with Multiple Formats

See also

Applies to

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.

public:
 Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file, ... cli::array <System::String ^> ^ delimiters);
public Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser (string file, params string[] delimiters);
member this.OpenTextFieldParser : string * string[] -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Function OpenTextFieldParser (file As String, ParamArray delimiters As String()) As TextFieldParser

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.

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.

To See
Read from a delimited text file How to: Read From Comma-Delimited Text Files
Read from a fixed-width text file How to: Read From Fixed-width Text Files
Read from a text file with multiple formats How to: Read From Text Files with Multiple Formats

See also

Applies to

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.

public:
 Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file);
public Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser (string file);
member this.OpenTextFieldParser : string -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Function OpenTextFieldParser (file As String) As TextFieldParser

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.

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.

To See
Read from a delimited text file How to: Read From Comma-Delimited Text Files
Read from a fixed-width text file How to: Read From Fixed-width Text Files
Read from a text file with multiple formats How to: Read From Text Files with Multiple Formats

See also

Applies to