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
Exceptions
The following conditions may cause an exception:
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 \\.\) (ArgumentException).
The file name ends with a trailing slash (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
Specified file does not exist (FileNotFoundException).
Specified file is in use (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
A row cannot be parsed using the specified format (MalformedLineException). The exception message specifies the line causing the exception, while the TextFieldParser.ErrorLine Property is assigned the text contained in the line.
The user does not have sufficient permissions to access the file (UnauthorizedAccessException).
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 |
|
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 TextFieldParserreader 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 |
---|---|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
|
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
Concepts
Parsing Text Files with the TextFieldParser Object