read a csv file in csharp

To read a CSV file in C#, you can use the TextFieldParser class available in the Microsoft.VisualBasic.FileIO namespace. Here's an example code snippet:

main.cs
using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic;

// ...

string filePath = @"C:\example.csv";
List<string[]> rows = new List<string[]>();

using (TextFieldParser parser = new TextFieldParser(filePath))
{
    parser.Delimiters = new string[] { "," };
    while (!parser.EndOfData)
    {
        string[] row = parser.ReadFields();
        rows.Add(row);
    }
}

// Now you can access the rows variable containing the CSV data
448 chars
20 lines

Alternatively, you can use third-party libraries like CsvHelper or the built-in OleDb provider to read CSV files in C#.

gistlibby LogSnag