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.
TextFieldParser
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.
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:
For more information, see Code Access Security and Requesting Permissions.
Tasks
Concepts
Reference
Other Resources