How to read a CSV file in C#

CSV (Comma-separated values) files are widely used for storing and exchanging tabular data due to their simplicity and ease of interpretation. CSV files are commonly utilized because they are compatible with Microsoft Excel, data extraction from them is straightforward, and their data can be effortlessly imported into a database.

Structure of a CSV file

Data is stored in a CSV file in a structured format. The first row usually represents the column’s name, and the following rows are the data. The items on each row are separated by a delimiter (commas, tab spaces, colons, semi-colons, etc.).

Here’s an example of how data is stored in a CSV file:

An example of what the CSV file looks like
An example of what the CSV file looks like

Steps required

C# allows importing data from a CSV file. This is one of the ways of how it is done:

  1. Import the System.IO namespace to enable I/O operations.

  2. Open and read the CSV file using the ReadAllLines() method from the System.IO namespace.

  3. Iterate through each line and for each line and store data in a suitable variable.

  4. Close the CSV file.

Code example

main.cs
Data.csv
using System;
using System.IO;
using System.Collections.Generic;
class ReadCsv
{
static List<string[]> ReadCsvFile(string filePath)
{
List<string[]> rows = new List<string[]>();
try
{
// Read all lines from the CSV file
string[] lines = File.ReadAllLines(filePath);
// Process each line and split by the comma to get individual values
foreach (string line in lines)
{
string[] values = line.Split(',');
// Add the values to the list of rows
rows.Add(values);
}
Console.WriteLine("CSV file read successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return rows;
}
static void Main()
{
// Specify the path to the CSV file
string csvFilePath = "Data.csv";
// Call the method to read and process the CSV data
List<string[]> csvData = ReadCsvFile(csvFilePath);
// Display the read data
foreach (string[] row in csvData)
{
Console.WriteLine(string.Join(", ", row));
}
}
}

Code explanation

  • Line 7: The function ReadCsvFile is being defined as receiving a file and returning its data in a list format.

  • Line 9: An empty instance of the string[] type list is initialized that is empty under the variable name rows.

  • Line 11: The try block is declared for error handling.

  • Line 14: The CSV file is read using the System.IO.ReadAllLines() method.

  • Lines 17–23: The file is traversed line by line, and each line is split into an array of values using the Split(<>) method and stored in a variable.

  • Line 25: A feedback message is displayed.

  • Lines 27–30: The catch block catches and displays any error.

  • Line 32: The read file is returned.

  • Line 35: The entry point i.e. the Main() method is declared.

  • Line 38: The path to the CSV file is specified.

  • Line 41: The function ReadCsvFile() is called, and the result is stored in a variable.

  • Lines 44–47: The CSV data is output in a table-like format.

Conclusion

Reading CSV files in C# is a fundamental task, and the provided example offers a solid foundation for our projects, particularly when dealing with data interchange between applications or when analyzing data stored in a tabular format. Additionally, reading CSV files is valuable in scenarios where data needs to be migrated, transformed, or integrated into databases, making it a practical tool for tasks involving data analysis, reporting, and system interoperability.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved