TextFieldParser.FieldWidths 屬性

定義

代表正在剖析文字檔中每一個資料行的寬度。

public:
 property cli::array <int> ^ FieldWidths { cli::array <int> ^ get(); void set(cli::array <int> ^ value); };
public int[]? FieldWidths { get; set; }
public int[] FieldWidths { get; set; }
member this.FieldWidths : int[] with get, set
Public Property FieldWidths As Integer()

屬性值

Int32[]

整數陣列,其中包含正在剖析之文字檔中每一個資料行的寬度。

例外狀況

任何位置 (除了陣列的最後一個項目以外) 中的寬度值會小於或等於零。

範例

本範例會讀取檔案 ParserText.txt,並指定寬度;第一欄寬為 5 個字元,第二欄為 10,第三欄為 11,第四欄為可變寬度

Using MyReader As New Microsoft.VisualBasic.FileIO.
    TextFieldParser("C:\ParserText.txt")

    MyReader.TextFieldType = 
        Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
    MyReader.FieldWidths = {5, 10, 11, -1}
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.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
End Using

備註

只有當 屬性設定為 FieldType.FixedWidth時,TextFieldType這個屬性才有意義。 如果陣列中的最後一個專案小於或等於零,則字段會假設為可變寬度。

方法 SetFieldWidths 也可以用來設定欄位寬度。

下表列出涉及 屬性的工作 FieldWidths 範例。

收件者 請參閱
從固定寬度文本檔讀取 作法:從固定寬度的文字檔讀取

適用於

另請參閱