Using CSV and Other Delimited Files in C#


This article describes delimited text files of data and in particular Comma-Separated Values (CSV) files. The accompanying sample program reads CSV files into a DataGridView, allows editing of the data, and saves the data back to the CSV file.

Delimited files have been used for many years to store data. In their simplest form, delimited files consist of records with a fixed number of fields in each record of the file. In Windows, records are ended by a carriage-return and line-feed (vbCrLf in VB) which can be coded in C# as "\r\n". Any delimiter can be used to separate fields; the most common delimiters are commas and tabs. When commas are used, the format is usually called Comma-Separated Values (CSV). The CSV format is extremely common but it has never been standardized. See Common Format and MIME Type for Comma-Separated Values (CSV) Files for a standard that applies to the internet, but for the internet, XML files are usually used instead of CSV files. CSV files are often considered to be Excel files, since Excel imports and exports data in CSV format but many other programs use CSV and CSV was used extensively before Excel was developed.

There are many tools for processing delimited text files. Many programs can import and export them. Older Microsoft database software reads and writes CSV files similar to reading and writing tables, but Microsoft is dropping support of the older database technologies. It is entirely possible to read and write delimited text files using plain File IO classes such as StreamReader and StreamWriter.

The CSV format has traditionally been directly supported by the Basic language, including Visual Basic. Delimited text files are not directly supported by .Net but the TextFieldParser class in the Microsoft.VisualBasic.FileIO namespace can read delimited text files and can be used by C# programs.

Microsoft.VisualBasic.FileIO namespace

The Microsoft.VisualBasic.FileIO namespace is not part of .Net; it is part of Visual Basic but it is available for use by C# programs too. There are many useful classes in it, including the TextFieldParser class. The TextFieldParser class can read most delimited text files, but it does not write them. Writing the data is easier than reading since reading the data requires a bit of parsing. The parsing is often trivial and when it is, it would be more efficient to just do the code in C#. If your data is more complex or if you want to just let the TextFieldParser class do it, the TextFieldParser class is easy to use. Note that since CSV is not a standard, you might encounter data that is not compatible with the Microsoft software.

To use the TextFieldParser class, be sure to add a reference to Microsoft.VisualBasic. Create an instance of the class then set the delimiters using SetDelimiters, as in:

Parser = new TextFieldParser(Filename);
Parser.SetDelimiters(",");
Then read records as in:
String[] Fields = null;
Fields = Parser.ReadFields();

Note that if the first record is a header record with field names, then you need to process it separately. The ReadFields method reads the fields of a record into a String array. Read the fields using ReadFields until EndOfData is true.

Sample Program

The sample program reads CSV files into a DataGridView, allows editing of the data, and saves the data back to the CSV file. The sample is not intended to be a complete application, but it can help get you started. The sample program writes the data using StreamWriter. All fields are written enclosed in double-quotes. You need to modify the code to satisfy your requirements.


Similar Articles