Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual Basic
 OpenTextFieldParser Method (My.Comp...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Basic Language Reference
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
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.

The following conditions may cause an exception:

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.

Visual Basic
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

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

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker