CsvHelper getting just the headers row

Miko picture Miko · Sep 16, 2014 · Viewed 10.6k times · Source

I am trying to read an uploaded CSV file and before doing anything with the data I need to check the first header name to be sure it is the correct file. I have been trying to find a way to do it but the reader skips to the second row instead. Is there a direct way of selecting one of the headers and checking its value?

Answer

Rick Jolly picture Rick Jolly · Apr 25, 2018

You can use the CsvReader to get the header row strings as described in this answer:

using (var csv = new CsvReader(reader))
{
    csv.Read();
    csv.ReadHeader();
    string[] headerRow = csv.Context.HeaderRecord;
}