Last active
March 23, 2016 18:14
-
-
Save dannylloyd/1167207 to your computer and use it in GitHub Desktop.
Read delimited text file in vb.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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