Posted on by & filed under Programming & Development.

Safari Books Online subscriber, experienced software developer and guest blogger, Kevin LaBranche, submitted this blog post for you to enjoy. Thank you, Kevin!

Need to parse a text file or perhaps a stream? Tired of writing parsing code to do this? Well, for only $19.95…. Ha, ha, just kidding. I ran across FileHelpers earlier this week and was impressed.

FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.

The idea is pretty simple:

You can strong type your flat file (fixed or delimited) simply describing a class that maps to each record and later read/write your file as an strong typed .NET array

The Library also has support for import/export data from differents storages like Excel, Access, SqlServer, etc. – Home page of

Setting it up and getting started is very easy. I’ll walk you through a sample.

I have a CSV file with usernames and email addresses in it. I need to parse that file and I need to verify the information in the CSV against another data store. I won’t go into verifying to the other data store for this example.

For the purpose of the exercise, create a new C# console project named FileHelpersSample.

Download version 2.0 and reference the FileHelpers library in your project.

Here’s how our CSV looks:


HINT – If you are following along and creating this project from scratch be sure to create a file named Users.csv with the above entries in it and put it in your debug/release bin folders.

In your project create a new class called User. This class will be used to map the CSV file.

Now create a new class and call it Parser. This class will invoke the FileHelpers engine to parse the file. It should look like this:

The FileHelperEngine uses generics to accept the mapping class. When the ReadFile method is called on the object it uses the mapping to return an array of User objects.

It’s that easy to parse a CSV file.

Now, let’s complete the sample. Open the Program.cs file and copy the code below:

Run the project and you should see:

That was easy!

The library can do a lot more than this simple example shows.  Be sure to read their quick starts and samples.  The library can ignore the first and last X number of lines as well as empty lines.  It can ignore/process records based on conditions you set along with many other options.  It even has a tool to create your mapping class.

Download this sample in C# or VB.

Happy Coding!

Guest Blogger Kevin LaBranche

For the last 11+ years I have worked as a developer for Coconino County and the last 3 as team manager.  I do freelance work as well when the right project comes around and time permits.  I am a CIS/CSE graduate of NAU and have been happy to call Flagstaff home for the past 16 years.  It’s a great town to raise a family and if you love the outdoors and all the seasons like I do, it can’t be beat!

I don’t just live and breathe code.  From a young age my Father instilled in me a love for the outdoors.  Camping, hiking, skiing, rock climbing, etc.  I also like to play disc golf, paintball and I enjoy fishing and archery.  If it involves the outdoors I probably like it.

Kevin on Twitter:

Kevin’s blog:

Tags: .NET, c# console, csv file, file parsing, filehelpers, kevin labranche, open source, Safari Books Online,

Comments are closed.