Read a csv file in C#


CSV is a file that contains data seperated by commas. In this tutorial, i will show you how to read a CSV file in C#.

Read CSV

For the purpose of this tutorial, we will use the below csv sample data.

CSV sample data

The file contains 16 records and each record contains 3 columns (Last name, First name and SSN) seperated by commas.
The following code illustrates how to read the above csv file.

// path to the csv file
string path = "C:/Users/overl/Desktop/people.csv";

string[] lines = System.IO.File.ReadAllLines(path);
foreach(string line in lines)
    string[] columns = line.Split(',');
    foreach (string column in columns) {
        // Do something

In preceding code, we use the ReadAllLines method of the File object to read all content of our csv file. ( the path of the csv file is passed as a parameter of the method). The ReadAllLines method returns a string array containing all lines of the file. Since the information contained in each line is seperated by a comma, we use the Split method in order to get that information.


To use the File object and its methods, you have to include the following namespace :

using System.IO;

See also