Click here to Skip to main content
15,112,453 members
Articles / Web Development / ASP.NET
Posted 22 Jun 2010


88 bookmarked

C# CSV Reader and Writer

Rate me:
Please Sign up or sign in to vote.
4.86/5 (16 votes)
22 Jun 2010MIT1 min read
Class library which provides the ability to read and write CSV files


I have lost count of the number of projects I have worked on that required me to write data in a CSV format and present it to the user for download. On a recent project, I decided that it would be a good idea to implement some classes that would simplify the process of reading from and writing to a CSV file.

I had a scan around on the web to find a simple solution to the problem. There were many classes that already do this, but I found them way too complex.

So I decided to build my own!

Using the Code

Below are a few samples of how the classes can be used. The classes have many constructor and method overloads that allow reading/writing from/to files, strings and streams.

Using the CsvReader Class

List<List<string>> records = new List<List<string>>();

using (CsvReader reader = new CsvReader(FilePath, Encoding.Default))
    while (reader.ReadNextRecord())

The CsvReader class has the following properties:

  • TrimColumns - Gets or sets whether column and heading values should be trimmed
  • HasHeaderRow - Gets or sets whether the CSV content has a header row
  • Fields - Gets the fields in the current Record
  • FieldCount - Gets the number of fields in the current record

Using the CsvFile Class

CsvFile file = new CsvFile();
file.Populate(FilePath, true);

The CsvFile class has the following properties:

  • Headers - Gets the column headers
  • Records - Gets the records within the CSV file
  • HeaderCount - Gets the number of header columns
  • RecordsCount - Gets the number of records within the CSV file

Using the CsvWriter Class

CsvFile csvFile = new CsvFile();
csvFile.Populate(FilePath, true);

using (CsvWriter writer = new CsvWriter())
   writer.WriteCsv(csvFile, FilePath);

The CsvWriter class has the following properties:

  • ReplaceCarriageReturnsAndLineFeedsFromFieldValues - Gets or sets whether carriage returns and line feeds should be replaced in field values.
  • CarriageReturnAndLineFeedReplacement - Gets or sets the character replacement for carriage returns and line feeds.


Version 1.0 (22-06-2010)

  • 1.0: First release


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Web Developer
United Kingdom United Kingdom
I have been developing web and software applications for 9 years originally in Microsoft Visual Fox Pro. But for the last 8 years I have been using Microsoft.Net.

I love writing code and especially enjoy writing tools to aid development.

I have developed my own code generator which generates C# code from a SQL Server database. The generator generates Business Components, Data Components and entities and is optimised for performance as it uses stored procedures and SqlDataReaders.

FrameworkGen can be downloaded for free from

Comments and Discussions

QuestionElegant and simple. Pin
me@vbman.com22-Apr-20 11:37
Memberme@vbman.com22-Apr-20 11:37 
QuestionGreat! Pin
tolstyi.toxa13-Dec-13 22:45
Membertolstyi.toxa13-Dec-13 22:45 
SuggestionReadNextRecord Pin
Alexander Kolenov22-Jun-12 2:30
MemberAlexander Kolenov22-Jun-12 2:30 
QuestionHow do I use CsvWrite to flush each row in a csv file Pin
Member 82787139-Dec-11 12:14
MemberMember 82787139-Dec-11 12:14 
Bugbug with empty feild Pin
noav6-Dec-11 1:39
Membernoav6-Dec-11 1:39 
BugRe: bug with empty feild Pin
izogi23-May-12 16:56
Memberizogi23-May-12 16:56 
GeneralRe: bug with empty feild Pin
jemnet28-Jan-13 9:06
Memberjemnet28-Jan-13 9:06 
GeneralRe: bug with empty feild Pin
Member 1013498421-Jul-14 11:17
MemberMember 1013498421-Jul-14 11:17 
GeneralMy vote of 5 Pin
Member 790749028-Aug-11 4:30
MemberMember 790749028-Aug-11 4:30 
Generalhere we go with a secound solution Pin
oscar@angress.de23-Feb-11 10:50
Memberoscar@angress.de23-Feb-11 10:50 
GeneralStyleCopped Version of this Project Pin
Tatworth21-Feb-11 11:23
MemberTatworth21-Feb-11 11:23 
GeneralMy vote of 5 Pin
Tatworth16-Feb-11 1:09
MemberTatworth16-Feb-11 1:09 
GeneralThanks! Pin
D-Three2-Nov-10 3:36
MemberD-Three2-Nov-10 3:36 
QuestionDid you miss this one? Pin
IceCreamWizard29-Jun-10 2:46
MemberIceCreamWizard29-Jun-10 2:46 
AnswerRe: Did you miss this one? Pin
CroweMan29-Jun-10 4:43
MemberCroweMan29-Jun-10 4:43 
AnswerRe: Did you miss this one? Pin
Andrew Rissing29-Jun-10 5:31
MemberAndrew Rissing29-Jun-10 5:31 
GeneralMy vote of 2 Pin
Marc Scheuner25-Jun-10 1:14
professionalMarc Scheuner25-Jun-10 1:14 
GeneralA long way to go PinPopular
Solid State Coder23-Jun-10 4:15
professionalSolid State Coder23-Jun-10 4:15 
GeneralRe: A long way to go Pin
flyingxu13-Dec-11 4:33
Memberflyingxu13-Dec-11 4:33 
GeneralNot enough code and explanation PinPopular
PIEBALDconsult22-Jun-10 19:03
professionalPIEBALDconsult22-Jun-10 19:03 
GeneralCSV Helpers Pin
Narshe22-Jun-10 8:23
MemberNarshe22-Jun-10 8:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.