Skip to content

Instantly share code, notes, and snippets.

@dannylloyd
Last active March 23, 2016 18:14
Show Gist options
  • Select an option

  • Save dannylloyd/1167207 to your computer and use it in GitHub Desktop.

Select an option

Save dannylloyd/1167207 to your computer and use it in GitHub Desktop.
Read delimited text file in vb.net
using (var reader = new TextFieldParser(@"C:\Users\Danny\Desktop\New Text Document.txt"))
{
reader.HasFieldsEnclosedInQuotes = true;
reader.SetDelimiters(new string[] { "," });
while ((!reader.EndOfData))
{
try
{
Console.WriteLine(reader.LineNumber);
string[] fields = reader.ReadFields();
foreach (string value in fields)
{
Console.WriteLine(value);
}
}
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
{
Console.WriteLine("line number " + reader.ErrorLineNumber + " had an error on it. The text is as follows : " + reader.ErrorLine);
}
}
}
Using var reader As New TextFieldParser("C:\Users\Danny\Desktop\New Text Document.txt")
reader.HasFieldsEnclosedInQuotes = True
reader.SetDelimiters(New String() {","})
While(Not reader.EndOfData)
Try
Console.WriteLine(reader.LineNumber)
Dim fields As String() = reader.ReadFields()
For Each value As String In fields
Console.WriteLine(value)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLine("line number " & reader.ErrorLineNumber & " had an error on it. The text is as follows : " & reader.ErrorLine)
End Try
End While
End Using
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment