Reputation: 91
Oke so i read an CSV file with streamreader and get the exception index out of range when it reads an empty line or when the line is not in the correct format.
is there an solution that when you get this error you go to the next line of the csv file
Upvotes: 1
Views: 3936
Reputation: 460288
The best approach is to avoid parsing CSV manually at all and use one of the available CSV-readers instead. For example this fast CSV-reader.
Instead of reacting on exceptions i would skip empty lines in the first place.
Instead of a StreamReader
you could also use File.ReadLines
with Linq:
Dim lines = From line In File.ReadLines(path)
Where line.Length <> 0
' now you can enumerate all not-empty lines '
For Each line In lines
' ... '
Next
If you insist on a Streamreader
:
Using sr = New StreamReader(path)
While Not sr.EndOfStream
Dim line = sr.ReadLine()
If Not String.IsNullOrEmpty(line) Then
' ... '
End If
End While
End Using
Upvotes: 1