FileSystem.OpenTextFieldParser 方法

定義

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

多載

OpenTextFieldParser(String, String[])

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

OpenTextFieldParser(String)

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

OpenTextFieldParser(String, Int32[])

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

OpenTextFieldParser(String, String[])

來源:
FileSystem.vb
來源:
FileSystem.vb
來源:
FileSystem.vb

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

public:
 static Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file, ... cli::array <System::String ^> ^ delimiters);
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser (string file, params string[] delimiters);
static member OpenTextFieldParser : string * string[] -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Shared Function OpenTextFieldParser (file As String, ParamArray delimiters As String()) As TextFieldParser

參數

file
String

要使用 TextFieldParser 開啟的檔案。

delimiters
String[]

欄位的分隔符號。

傳回

TextFieldParser,會讀取指定的檔案。

例外狀況

路徑無效,原因如下:它是長度為零的字串;它只包含空格符;它包含無效的字元;或它是裝置路徑, (開頭為 \\.\) ;結尾為斜線。

fileNothing

檔案不存在。

檔案正由另一個程序使用中,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱含有冒號 (:),或者是無效的格式。

無法使用指定的格式剖析資料列。 例外狀況訊息指出造成例外狀況的文字行,而 ErrorLine 屬性被指派至包含於該文字行中的文字。

使用者缺乏必要的使用權限來檢視路徑。

範例

此範例會開啟 TextFieldParser.reader 並將其用於從 C:\TestFolder1\Test1.txt 讀取。

Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
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

備註

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

收件者 請參閱
從分隔的文字檔讀取 如何:在 Visual Basic 中從 Comma-Delimited 文字檔讀取
從固定寬度文本檔讀取 如何:從 Visual Basic 中的固定寬度文本檔讀取
從具有多種格式的文字檔讀取 如何:在 Visual Basic 中以多種格式從文字檔讀取

另請參閱

適用於

OpenTextFieldParser(String)

來源:
FileSystem.vb
來源:
FileSystem.vb
來源:
FileSystem.vb

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

public:
 static Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file);
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser (string file);
static member OpenTextFieldParser : string -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Shared Function OpenTextFieldParser (file As String) As TextFieldParser

參數

file
String

要使用 TextFieldParser 開啟的檔案。

傳回

TextFieldParser,會讀取指定的檔案。

例外狀況

路徑無效,原因如下:它是長度為零的字串;它只包含空格符;它包含無效的字元;或它是裝置路徑, (開頭為 \\.\) ;結尾為斜線。

fileNothing

檔案不存在。

檔案正由另一個程序使用中,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱含有冒號 (:),或者是無效的格式。

無法使用指定的格式剖析資料列。 例外狀況訊息指出造成例外狀況的文字行,而 ErrorLine 屬性被指派至包含於該文字行中的文字。

使用者缺乏必要的使用權限來檢視路徑。

範例

此範例會開啟 TextFieldParser.reader 並將其用於從 C:\TestFolder1\Test1.txt 讀取。

Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
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

備註

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

收件者 請參閱
從分隔的文字檔讀取 如何:在 Visual Basic 中從 Comma-Delimited 文字檔讀取
從固定寬度文本檔讀取 如何:從 Visual Basic 中的固定寬度文本檔讀取
從具有多種格式的文字檔讀取 如何:在 Visual Basic 中以多種格式從文字檔讀取

另請參閱

適用於

OpenTextFieldParser(String, Int32[])

來源:
FileSystem.vb
來源:
FileSystem.vb
來源:
FileSystem.vb

OpenTextFieldParser 方法能讓您建立 TextFieldParser 物牛,而這個物件有助於輕鬆有效率地剖析結構化文字檔 (例如記錄檔)。 可以使用 TextFieldParser 物件,讀取分隔的和固定寬度的檔案。

public:
 static Microsoft::VisualBasic::FileIO::TextFieldParser ^ OpenTextFieldParser(System::String ^ file, ... cli::array <int> ^ fieldWidths);
public static Microsoft.VisualBasic.FileIO.TextFieldParser OpenTextFieldParser (string file, params int[] fieldWidths);
static member OpenTextFieldParser : string * int[] -> Microsoft.VisualBasic.FileIO.TextFieldParser
Public Shared Function OpenTextFieldParser (file As String, ParamArray fieldWidths As Integer()) As TextFieldParser

參數

file
String

要使用 TextFieldParser 開啟的檔案。

fieldWidths
Int32[]

欄位的寬度。

傳回

TextFieldParser,會讀取指定的檔案。

例外狀況

路徑無效,原因如下:它是長度為零的字串;它只包含空格符;它包含無效的字元;或它是裝置路徑, (開頭為 \\.\) ;結尾為斜線。

fileNothing

檔案不存在。

檔案正由另一個程序使用中,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱含有冒號 (:),或者是無效的格式。

無法使用指定的格式剖析資料列。 例外狀況訊息指出造成例外狀況的文字行,而 ErrorLine 屬性被指派至包含於該文字行中的文字。

使用者缺乏必要的使用權限來檢視路徑。

範例

此範例會開啟 TextFieldParser.reader 並將其用於從 C:\TestFolder1\Test1.txt 讀取。

Dim reader = My.Computer.FileSystem.OpenTextFieldParser(
    "C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.delimiters = {","}
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

備註

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

收件者 請參閱
從分隔的文字檔讀取 如何:在 Visual Basic 中從 Comma-Delimited 文字檔讀取
從固定寬度文本檔讀取 如何:從 Visual Basic 中的固定寬度文本檔讀取
從具有多種格式的文字檔讀取 如何:在 Visual Basic 中以多種格式從文字檔讀取

另請參閱

適用於