My.Computer.FileSystem.OpenTextFieldParser 方法

更新:2007 年 11 月

OpenTextFieldParser 方法可用于创建 TextFieldParser 对象,该对象提供一种简单、高效的方式来分析结构化文本文件(如日志)。TextFieldParser 对象可用于读取符号分隔的文件和固定宽度的文件。

' 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。要用 TextFieldParser 打开的文件。必选。

  • delimiters
    String()。用作字段的分隔符。必选。

  • fieldWidths
    Integer()。字段的宽度。必选。

返回值

TextFieldParser

异常

以下情况可能会导致异常:

任务

下表列出了涉及 My.Computer.FileSystem.OpenTextFieldParser 方法的任务的示例。

要执行的操作

请参见

从符号分隔的文本文件中读取

如何:在 Visual Basic 中读取逗号分隔的文本文件

从固定宽度的文本文件中读取

如何:在 Visual Basic 中读取固定宽度的文本文件

从具有多种格式的文本文件中读取

如何:在 Visual Basic 中读取具有多种格式的文本文件

示例

此示例打开 TextFieldParserreader,并使用它从 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

要求

命名空间:Microsoft.VisualBasic.MyServices

类:FileSystemProxy(提供对 FileSystem 的访问)

**程序集:**Visual Basic 运行库(位于 Microsoft.VisualBasic.dll 中)

不同项目类型的可用性

项目类型

是否可用

Windows 应用程序

类库

控制台应用程序

Windows 控件库

Web 控件库

Windows 服务

网站

权限

可能需要下列权限:

权限

说明

FileIOPermission

控制访问文件和文件夹的能力。关联的枚举:Unrestricted

SecurityPermission

描述应用于代码的一组安全权限。关联的枚举:ControlEvidence

有关更多信息,请参见代码访问安全性请求权限

请参见

任务

如何:在 Visual Basic 中读取文本文件

如何:在 Visual Basic 中读取逗号分隔的文本文件

如何:在 Visual Basic 中读取具有多种格式的文本文件

概念

使用 TextFieldParser 对象分析文本文件

参考

My.Computer.FileSystem 对象

TextFieldParser 对象

OpenTextFieldParser

其他资源

读取文件 (Visual Basic)