.NET Framework Class Library
TextFieldParser Class

Provides methods and properties for parsing structured text files.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
Syntax

Visual Basic (Declaration)
Public Class TextFieldParser _
    Implements IDisposable
Visual Basic (Usage)
Dim instance As TextFieldParser
C#
public class TextFieldParser : IDisposable
Visual C++
public ref class TextFieldParser : IDisposable
JScript
public class TextFieldParser implements IDisposable
Remarks

For more detailed information, see the Visual Basic topic TextFieldParser Object.

The TextFieldParser object provides methods and properties for parsing structured text files. Parsing a text file with the TextFieldParser is similar to iterating over a text file, while the ReadFields method to extract fields of text is similar to splitting the strings.

The TextFieldParser can parse two types of files: delimited or fixed-width. Some properties, such as Delimiters and HasFieldsEnclosedInQuotes are meaningful only when working with delimited files, while the FieldWidths property is meaningful only when working with fixed-width files.

The following table lists examples of tasks involving the Microsoft.VisualBasic.FileIO.TextFieldParser object.

Examples

This example parses through a tab-delimited text file, Bigfile.

Visual Basic
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\logs\bigfile")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {vbTab}
    Dim currentRow As String()
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row.
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & _
            " is invalid.  Skipping")
        End Try
    End While
End Using

This example depends on the existence of a function, processFields, which processes the fields as they are read.

Inheritance Hierarchy

System..::.Object
  Microsoft.VisualBasic.FileIO..::.TextFieldParser
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Microsoft.VisualBasic.FileIO..::.TextFieldParser

Other Resources

Page view tracker