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(). 필드에 대한 구분 기호입니다.

  • fieldWidths
    필수적 요소. Integer(). 필드의 너비입니다.

반환 값

TextFieldParser

예외

다음 조건에서 예외가 발생할 수 있습니다.

  • 경로가 길이가 0인 문자열이거나, 공백만 포함하거나, 잘못된 문자를 포함하거나, \\.\로 시작하는 장치 경로이기 때문에 올바르지 않는 경우(ArgumentException)

  • 파일 이름이 슬래시로 끝나는 경우(ArgumentException)

  • 경로가 Nothing이기 때문에 올바르지 않은 경우(ArgumentNullException)

  • 지정된 파일이 없는 경우(FileNotFoundException)

  • 지정된 파일이 사용되고 있는 경우(IOException)

  • 경로의 길이가 시스템에서 정의한 최대 길이를 초과하는 경우(PathTooLongException)

  • 경로의 파일 이름이나 디렉터리 이름에 콜론(:)이 있거나 이름의 형식이 잘못된 경우(NotSupportedException)

  • 경로를 보는 데 필요한 권한이 사용자에게 없는 경우(SecurityException)

  • 지정된 형식을 사용하여 행을 구문 분석할 수 없는 경우(MalformedLineException). 이 예외 메시지는 예외를 발생시키는 줄을 지정하며 해당 줄에 포함된 텍스트에는 TextFieldParser.ErrorLine 속성이 할당됩니다.

  • 파일에 액세스할 수 있는 충분한 권한이 사용자에게 없는 경우(UnauthorizedAccessException)

작업

다음 표에서는 My.Computer.FileSystem.OpenTextFieldParser 메서드와 관련된 작업의 예를 보여 줍니다.

작업

참조

구분된 텍스트 파일에서 읽기

방법: Visual Basic에서 쉼표로 구분된 텍스트 파일 읽기

고정 폭 텍스트 파일에서 읽기

방법: Visual Basic에서 고정 너비 텍스트 파일 읽기

여러 형식이 있는 텍스트 파일에서 읽기

방법: 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

요구 사항

네임스페이스:Microsoft.VisualBasic.MyServices

클래스:FileSystemProxy(FileSystem에 대한 액세스 제공)

어셈블리: Visual Basic 런타임 라이브러리(Microsoft.VisualBasic.dll)

프로젝트 형식별 사용 가능 여부

프로젝트 형식

사용 가능 여부

Windows 응용 프로그램

클래스 라이브러리

콘솔 응용 프로그램

Windows 컨트롤 라이브러리

웹 컨트롤 라이브러리

Windows 서비스

웹 사이트

권한

다음과 같은 사용 권한이 필요합니다.

권한

설명

FileIOPermission

파일 및 폴더에 대한 액세스 가능성을 제어합니다. 연관된 열거형: Unrestricted

SecurityPermission

코드에 적용된 보안 권한 집합을 설명합니다. 연관된 열거형: ControlEvidence

자세한 내용은 코드 액세스 보안권한 요청을 참조하십시오.

참고 항목

작업

방법: Visual Basic에서 텍스트 파일 읽기

방법: Visual Basic에서 쉼표로 구분된 텍스트 파일 읽기

방법: Visual Basic에서 여러 형식의 텍스트 파일 읽기

개념

TextFieldParser 개체를 사용하여 텍스트 파일 구문 분석

참조

My.Computer.FileSystem 개체

TextFieldParser 개체

OpenTextFieldParser

기타 리소스

Visual Basic에서 파일 읽기