共用方式為


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()。欄位的分隔符號 (Delimiter)。必要項。

  • fieldWidths
    Integer()。欄位的寬度。必要項。

傳回值

TextFieldParser

例外狀況

下列情形可能會造成例外狀況:

工作

下表列出包含 My.Computer.FileSystem.OpenTextFieldParser 方法的工作範例。

若要

請參閱

從有分隔符號的文字檔讀取

HOW TO:在 Visual Basic 中從逗號分隔文字檔讀取

讀取固定寬度的文字檔

HOW TO:在 Visual Basic 中從固定寬度的文字檔讀取

以多種格式讀取文字檔

HOW TO:在 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

需求

命名空間 (Namespace)︰Microsoft.VisualBasic.MyServices

類別 (Class):FileSystemProxy (提供對 FileSystem 的存取)

組件:Visual Basic 執行階段程式庫 (在 Microsoft.VisualBasic.dll 中)

依專案類型的可用性

專案類型

是否可用

Windows 應用程式

類別庫

主控台應用程式

Windows 控制項程式庫

Web 控制項程式庫

Windows 服務

網站

使用權限

下列使用權限可能為必要:

使用權限

描述

FileIOPermission

控制存取檔案和資料夾的功能。關聯的列舉型別:Unrestricted

SecurityPermission

描述套用至程式碼的一組安全性權限。關聯的列舉型別:ControlEvidence

如需詳細資訊,請參閱程式碼存取安全性要求使用權限

請參閱

工作

HOW TO:在 Visual Basic 中從文字檔讀取

HOW TO:在 Visual Basic 中從逗號分隔文字檔讀取

HOW TO:在 Visual Basic 中以多種格式從文字檔讀取

概念

使用 TextFieldParser 物件剖析文字檔

參考

My.Computer.FileSystem 物件

TextFieldParser 物件

OpenTextFieldParser

其他資源

在 Visual Basic 中讀取檔案