Some application uses a comma-delimited files for storing their temporary data. Developers using Visual Basic needs to read the file by line by line and then split the value using the “,”. This was some time useful and tedious. And the .Net has the beautiful TextFieldParser Class to read the comma-delimited values using .Net.
This TextFieldParser Class is located in Microsoft.VisualBasic.IO name space of the .Net Framework.
The below examples read the sample CSV file using VB.Net
Sample File :
"REVIEW_DATE","AUTHOR","ISBN","DISCOUNTED_PRICE" "1985/01/21","Douglas Adams",0345391802,5.95 "1990/01/12","Douglas Hofstadter",0465026567,9.95 "1998/07/15","Timothy ""The Parser"" Campbell",0968411304,18.99 "1999/12/03","Richard Friedman",0060630353,5.95 "2001/09/19","Karen Armstrong",0345384563,9.95 "2002/06/23","David Jones",0198504691,9.95 "2002/06/23","Julian Jaynes",0618057072,12.50 "2003/09/30","Scott Adams",0740721909,4.95 "2004/10/04","Benjamin Radcliff",0804818088,4.95 "2004/10/04","Randel Helms",0879755725,4.50
Code:
Dim parser As New FileIO.TextFieldParser("D:\Tests.txt")
Dim rows As String()
parser.SetDelimiters(",")
parser.TextFieldType = FileIO.FieldType.Delimited
Try
'Try to read the file
'Read the Header data
rows = parser.ReadFields
'print the Header
For Each str As String In rows
Console.WriteLine(str)
Next
'Read the row data
Do While Not parser.EndOfData
rows = parser.ReadFields
'Print the Results
For Each Str As String In rows
Console.WriteLine(Str)
Next
Loop
Catch ex As FileIO.MalformedLineException
MessageBox.Show(ex.Message)
Catch ex As Exception
'Show the Error Message
MessageBox.Show(ex.Message)
End Try
This part of the code read only the header information( First line of Header file ) and
display them in the console window.
Code:
rows = parser.ReadFields 'print the Header For Each str As String In rows Console.WriteLine(str) Next
Output:
REVIEW_DATE AUTHOR ISBN DISCOUNTED_PRICE
This read the values by line by line,split them using “,” and display it in console window.
Code:
'Read the row data
Do While Not parser.EndOfData
rows = parser.ReadFields
'Print the Results
For Each Str As String In rows
Console.WriteLine(Str)
Next
Loop
Output:
1985/01/21 Douglas Adams 0345391802 5.95 1990/01/12 Douglas Hofstadter 0465026567 9.95 1998/07/15 Timothy "The Parser" Campbell 0968411304 18.99 1999/12/03 Richard Friedman 0060630353 5.95
Now what happens when the file is not in the proper format. This is handled by the exception module.
I used this text for parsing
Dim text = "I, Need, Coffee, Before, I, ""Fall, Asleep"" Otherwise, ""I'll, Die""" ',
and it gave the exception as
"Line 1 cannot be parsed using the current Delimiters."
If you still need the detailed explanation then you can use the MalformatedLineException Class
Catch ex As FileIO.MalformedLineException MessageBox.Show(ex.Message)
If the source file does not exist then File not found exception will be thrown , this has to be handled


You have really interesting blog, keep up posting such informative posts!
thanx!!this helped alot!!!thnx so much