Did you know? A .NET CSV parser that comes with Visual Studio?

In the Microsoft.VisualBasic.FileIO (Microsoft.VisualBasic.dll) namespace lives the TextFieldParser class.

This useful little class can be used to parse structured text files – either delimited or fixed length. You can iterate over the lines in the file and extract the data through the ReadFields method.

Since it is provided by Microsoft, you can use it in environments that do not allow “third-party” libraries and as it is a .NET libarary you can use it in any .NET language (yes, C# and F# included) – just import the library.

The examples on MSDN are all in VB.NET, but are easily translated to other .NET dialects.

How to: Read From Comma-Delimited Text Files
How to: Read From Fixed-width Text Files
How to: Read From Text Files with Multiple Formats

4 Responses

  1. Jim Schubert April 21, 2012 / 23:10

    Although I don’t like Visual Basic syntax, there seem to be a lot of goodies in Microsoft.VisualBasic namespaces. I was thinking the other day how great it would be if the FCL included a utility to split lines in a way similar to awk, and this looks pretty close. Thanks for pointing it out!

  2. Shai Cohen February 25, 2013 / 20:36

    Congrats on your new job at StackOverflow. It. Just. Makes. Sense.

  3. Neolisk March 5, 2013 / 19:43

    The only problem is performance.

  4. Oded Coster March 5, 2013 / 20:03

    It may be a problem. You are making a sweeping statement here. Performance issues should be tackled if they are an issue (and only after measurements have shown where the bottlenecks are). There are faster parsers out there, but not built in.

Leave a Reply

Your email address will not be published. Required fields are marked *