TextFieldParser.ReadFields 메서드

정의

현재 줄의 모든 필드를 읽고 문자열 배열로서 반환한 다음 데이터가 포함된 다음 줄로 커서를 이동합니다.

public:
 cli::array <System::String ^> ^ ReadFields();
public string[]? ReadFields ();
public string[] ReadFields ();
member this.ReadFields : unit -> string[]
Public Function ReadFields () As String()

반환

String[]

현재 줄의 필드 값이 들어 있는 문자열 배열입니다.

예외

지정된 형식을 사용하여 필드를 구문 분석할 수 없는 경우

예제

이 예제에서는 메서드를 ReadFields 사용하여 쉼표로 구분된 파일 ParserText.txt에서 읽습니다. 이 예제에서는 필드를 Testfile.txt에 씁니다.

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {","}
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            For Each currentField As String In currentRow
                My.Computer.FileSystem.WriteAllText(
                    "C://testfile.txt", currentField, True)
            Next
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try
    End While
End Using

설명

사용자가 여러 형식 ReadFields 으로 텍스트 파일을 구문 분석할 수 있도록 하기 위해 메서드는 호출될 때마다 , DelimitersFieldWidthsTextFieldType값을 검사합니다. 사용자는 및 FieldWidths 또는 Delimiters 속성을 적절하게 올바르게 구성 TextFieldType 해야 합니다. TextFieldType 가 로 Delimited설정되고 Delimiters 가 설정되지 않았거나 가 및 FieldWidthsFixedWidth 설정된 경우 TextFieldType 예외가 throw됩니다.

빈 줄이 발견되면 ReadFields 건너뛰고 비어 있는 다음 줄이 반환됩니다.

메서드가 ReadFields 현재 줄을 구문 분석할 수 없는 경우 예외가 발생하며 다음 줄로 이동하지 않습니다. 이 통해 애플리케이션을 다시 줄을 구문 분석 하려고 합니다.

다음 표에서 관련 된 작업의 예제는 ReadFields 메서드.

대상 참조 항목
구분된 파일에서 읽기 방법: 쉼표로 구분된 텍스트 파일에서 읽기
고정 너비 파일에서 읽기 방법: 고정 너비 텍스트 파일에서 읽기

적용 대상

추가 정보