次の方法で共有


TextFieldParser.FieldWidths プロパティ

更新 : 2007 年 11 月

解析対象のテキスト ファイルに含まれる各列の幅を示します。

' Usage
Dim value As Integer() = TextFieldParserObject.FieldWidths
' Declaration
Public Property FieldWidths As Integer()

戻り値

Integer ().

例外

例外を引き起こす可能性のある状態を次に示します。

  • 配列の最後のエントリ以外の場所で、幅の値が 0 以下になっている (ArgumentException)。

解説

このプロパティは、TextFieldParser.TextFieldType プロパティ = FieldType.FixedWidth の場合にのみ意味を持ちます。配列の最後のエントリが 0 以下の場合は、そのフィールドは可変長と見なされます。

フィールドの幅を設定するには、SetFieldWidths メソッドも使用できます。詳細については、「TextFieldParser.SetFieldWidths メソッド」を参照してください。

処理手順

FieldWidths プロパティに関連するタスクの例を次の表に示します。

目的

参照項目

固定幅のテキスト ファイルから読み込む

方法 : Visual Basic で固定幅のテキスト ファイルを読み取る

使用例

この例では、列の幅を指定して ParserText.txt ファイルを読み取ります。列の幅は、1 列目は 5 文字、2 列目は 10 文字、3 列目は 11 文字、4 列目は可変長とします。

Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
    MyReader.FieldWidths = New Integer() {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

必要条件

名前空間 : Microsoft.VisualBasic.FileIO

クラス : TextFieldParser

アセンブリ : Visual Basic ランタイム ライブラリ (Microsoft.VisualBasic.dll 内)

アクセス許可

アクセス許可は不要です。

参照

処理手順

方法 : Visual Basic で固定幅のテキスト ファイルを読み取る

方法 : Visual Basic でコンマ区切りのテキスト ファイルを読み取る

方法 : Visual Basic で複数の書式を持つテキスト ファイルを読み取る

例外のトラブルシューティング : Microsoft.VisualBasic.FileIO.TextFieldParser.MalformedLineException

概念

TextFieldParser オブジェクトによるテキスト ファイルの解析

参照

TextFieldParser オブジェクト

TextFieldParser.FieldWidths

FieldType 列挙型