I’m using the A Fast CSV Reader library for reading CSV files in my C# application. However I’m having some trouble with creating the CsvReader object.
CsvReader csv = new CsvReader(new StreamReader("data.csv"), true));
When passing a binary file to this code no exception is thrown and the input file is treated as a normal CSV file. How can I detect if the given file is a CSV file? Or at least how can I make sure that it is not a binary file?
Detecting if a file is text or binary is hard. It also stands to reason that the vast majority of text files are also not CSV. As such, I question whether this validation is worthwhile.
If the CSV reader can’t detected trash inputs (perhaps when you start the parse rather than at construction), then I’d consider a different implementation. There are loads to choose from.
Here’s one available in .net:
Reference
Microsoft.VisualBasicand you can use TextFieldParser