Most customers will ask that your report be printed in PDF format. From this example, you do not have to install any third-party controls to create the PDF file.This example converts a text file and creates a PDF file. Downloads Download source code - 6 Kb Download demo project - 13 Kb This article was originally published on November 14th, 2003.
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
-
Create a new
TextFieldParser
. The following code creates theTextFieldParser
namedMyReader
and opens the filetest.txt
. -
Define the
TextField
type and delimiter. The following code defines theTextFieldType
property asDelimited
and the delimiter as ','. -
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.
-
Close the
While
andUsing
blocks withEnd While
andEnd Using
.
Example
This example reads from the file
test.txt
.
Robust programming
The following conditions may cause an exception:
-
A row cannot be parsed using the specified format (MalformedLineException). The exception message specifies the line causing the exception, while the ErrorLine property is assigned the text contained in the line.
-
The specified file does not exist (FileNotFoundException).
-
A partial-trust situation in which the user does not have sufficient permissions to access the file. (SecurityException).
-
The path is too long (PathTooLongException).
-
The user does not have sufficient permissions to access the file (UnauthorizedAccessException).