共用方式為


How to: Read From Comma-Delimited Text Files in Visual Basic

The TextFieldParser object provides a way to easily and efficiently parse structured text files, such as logs. The TextFieldType property defines whether it is a delimited file or one with fixed-width fields of text.

To parse a comma delimited text file

  1. Create a new TextFieldParser. The following code creates the TextFieldParser named MyReader and opens the file test.txt.

    Using MyReader As New Microsoft.VisualBasic.
                          FileIO.TextFieldParser(
                            "C:\TestFolder\test.txt")
    
  2. Define the TextField type and delimiter. The following code defines the TextFieldType property as Delimited and the delimiter as ",".

    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
    
  3. Loop through the fields in the file. If any lines are corrupt, report an error and continue parsing. The following code loops through the file, displaying each field in turn and reporting any fields that are formatted incorrectly.

    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
    
  4. Close the While and Using blocks with End While and End Using.

    End While 
    End Using
    

Example

This example reads from the file test.txt.

Using MyReader As New Microsoft.VisualBasic.
                        FileIO.TextFieldParser(
                          "C:\TestFolder\test.txt")
   MyReader.TextFieldType = FileIO.FieldType.Delimited
   MyReader.SetDelimiters(",")
   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

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Read From Fixed-width Text Files in Visual Basic

How to: Read From Text Files with Multiple Formats in Visual Basic

Walkthrough: Manipulating Files and Directories in Visual Basic

Troubleshooting: Reading from and Writing to Text Files (Visual Basic)

Troubleshooting Exceptions: Microsoft.VisualBasic.FileIO.TextFieldParser.MalformedLineException

Reference

Microsoft.VisualBasic.FileIO.TextFieldParser

Concepts

Parsing Text Files with the TextFieldParser Object (Visual Basic)