How do I read or parse comma separated files (CSV, TSV) in .NET? Use the TextFieldParser class
Posted: (EET/GMT+2)
When writing applications, I find that almost every application I need at least some kind of data exporting or reporting functionality. When implementing these, the CSV text based files format (Comma Separated Values, sometimes also Tab Separated Values or TSV) are the lowest common level, which works surprisingly well in many situations.
Also, the file format is simple enough to write by hand-coding some C#, but, if you want to do it properly, it gets quickly complex. Since CSV or TSV data generation hardly is any application's core business, I usually end up using .NET built-in class called TextFieldParser.
This little-known class allows you to process CSV or TSV files, and both parse (read) and write them. Since the class is part of the Visual Basic file I/O classes, not many C# developers have ventured into those spheres, and thus are missing out.
With this blog post, I hope I've introduced something useful and even new to my readers. Hope this helps!