Working with CSV in C# (.NET): Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with CSV in C# (.NET): Complete Guide



CSV (Comma-Separated Values) is one of the most common formats for exchanging tabular data. In the C# (.NET) ecosystem, there are several approaches to working with CSV files: from manual parsing to using powerful third-party libraries. In this article, we will cover all the main ways to read, write, and process CSV data in .NET.



1. Manual CSV Parsing with StreamReader



The simplest way is to use the standard StreamReader and String.Split classes. However, this method does not handle complex cases (quotes, commas inside data, line breaks).



using System;using System.IO;using System.Linq;

public class CsvSimpleReader{ public void ReadCsv(string filePath) { using (var reader = new StreamReader(filePath)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); Console.WriteLine(string.Join(" | ", values)); } } }}


Disadvantages: does not handle escaped commas and quotes. For real projects, use more reliable methods.



2. Using Microsoft.VisualBasic.FileIO.TextFieldParser (for .NET Framework)



In .NET Framework, there is a built-in CSV parser — TextFieldParser. It correctly handles quotes and delimiters.



using Microsoft.VisualBasic.FileIO;using System;using System.IO;

public class CsvTextFieldParser{ public void ParseCsv(string filePath) { using (var parser = new TextFieldParser(filePath)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(",");

while (!parser.EndOfData) { string[] fields = parser.ReadFields(); Console.WriteLine(string.Join(" | ", fields)); } } }}


Important: In .NET Core / .NET 5+, this class is available via the Microsoft.VisualBasic package (NuGet).



3. CsvHelper Library (Recommended Approach)



CsvHelper is the most popular library for working with CSV in .NET. It supports mapping to classes, LINQ queries, delimiter and encoding configuration.



Installation via NuGet:

Install-Package CsvHelper


3.1 Reading CSV into a List of Objects



using System.Globalization;using CsvHelper;using CsvHelper.Configuration;using System.IO;using System.Linq;using System.Collections.Generic;

public class Person{ public string Name { get; set; } public int Age { get; set; } public string Email { get; set; }}

public void ReadCsvWithCsvHelper(string filePath){ var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = true, Delimiter = ",", MissingFieldFound = null };

using (var reader = new StreamReader(filePath)) using (var csv = new CsvReader(reader, config)) { var records = csv.GetRecords<Person>().ToList(); foreach (var person in records) { Console.WriteLine($"{person.Name}, {person.Age}, {person.Email}"); } }}


3.2 Writing CSV from a Collection of Objects



public void WriteCsvWithCsvHelper(string filePath, List<Person> people){    using (var writer = new StreamWriter(filePath))    using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))    {        csv.WriteRecords(people);    }}


3.3 Manual Field Management (without Mapping)



public void ManualCsvWrite(string filePath){    using (var writer = new StreamWriter(filePath))    using (va                    

Recommendations