My.Computer.FileSystem.OpenTextFieldParser Method 

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.

' Usage
Dim value As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(file)
Dim value As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(file ,delimiters)
Dim value As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(file ,fieldWidths)
' Declaration
Public Function OpenTextFieldParser( _
   ByVal file As String _
) As TextFieldParser
' -or-
Public Function OpenTextFieldParser( _
   ByVal file As String, _
   ByVal delimiters As String() _
) As TextFieldParser
' -or-
Public Function OpenTextFieldParser( _
   ByVal file As String, _
   ByVal fieldWidths As Integer() _
) As TextFieldParser

Parameters

  • file
    String. The file to be opened with the TextFieldParser. Required.
  • delimiters
    String(). Delimiters for the fields. Required.
  • fieldWidths
    Integer(). Widths of the fields. Required.

Return Value

TextFieldParser

Exceptions

The following conditions may cause an exception:

Tasks

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

Example

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

Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser
reader = My.Computer.FileSystem.OpenTextFieldParser _
("C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = New String() {","}
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

Requirements

Namespace: Microsoft.VisualBasic.MyServices

Class: FileSystemProxy (provides access to FileSystem)

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

Availability by Project Type

Project type Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permissions may be necessary:

Permission Description

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.

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

See Also

Tasks

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

Reference

My.Computer.FileSystem Object
TextFieldParser Object
OpenTextFieldParser

Concepts

Parsing Text Files with the TextFieldParser Object

Other Resources

Reading from Files in Visual Basic