Need to read comma separated text files?

Then you really need to take a look at the TextFieldParser class. And if you never run into it that might be because it is tucked away in the Microsoft.VisualBasic.FileIO namespace, not exactly the first place I tend to start looking. But it parses either delimited or fixed with files. It has loads of options like setting the delimiter characters or specifying whether the data is enclosed in quotes or not. A real useful class to gave lying around given the number of questions I hear about reading CSV type files.

And yes C# diehards out there, just add a reference to Microsoft.VisualBasic.dll and you can use the type just as well [:)]


5 thoughts on “Need to read comma separated text files?

  1. Don’t you know what is the reason that TextFieldParser lives in Microsoft.VisualBasic.FileIO, but not somewhere in common namespaces? I wouldn’t have had to write my own if I knew where to find it 🙂

  2. Well my guess is that it all comes down to ownership if namespaces. I know the Membership or Caching API’s that lives in System.Web is just as useful in a WinForms application. However it was developed by the ASP.NET team and they had to put it in the Syetm.Web namespace. My guess is the same happened here where the VB team created a very useful class and didn’t have permission to put it in System.IO where it really should be.

    Just my guess though and there could very well be other reasons I am unaware of.

  3. Hi,
    If i want to find out the total number of lines a csv file has before i create the string array to store the content, what can i do to achieve it?

  4. Hi Jason,

    Sorry for the delay but I just got back from my vacation.

    The only way would be to read the file and count the number of lines. Sort of means reading the file twice though.

Leave a Reply

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