36

I am trying to use CsvHelper for a project. I went through the documentation but I couldn't find a way to read all the column names with a single method. How can I get a list of all column header names easily with CsvHelper? I am doing it like this currently but I assume there is a better way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using System.IO;

namespace Kg
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var reader = new StreamReader(@"myfile.csv"))
            {
                var csv = new CsvReader(reader);

                csv.Read();                  
                var has_header = true;
                var csv_headers = new List<string>();
                var header_index = 0;
                while (has_header)
                {
                    has_header = csv.TryGetField<string>(header_index, out string header_name);

                    if (has_header)
                    {
                        header_index += 1;
                        csv_headers.Add(header_name);

                    }

                }

                Console.WriteLine(csv_headers.Count);

            }
        }
}
B A
  • 1,089
  • 2
  • 14
  • 24
  • *Usually* you know what the data represents, so if you create a class to hold the imported data, CSVHelper will create typed objects for you and put them in a collection. – Ňɏssa Pøngjǣrdenlarp Jan 17 '18 at 18:16
  • 1
    As far as I know the header of a csv file is just the first row. – derloopkat Jan 17 '18 at 18:20
  • @Plutonix, thanks yes I agree, but there are more than 50 columns and I want to make a reusable class where I can just get the use the header names by parsing the csv file and setting the type based on the first few rows of each column. – B A Jan 17 '18 at 18:20
  • @derloopkat yes and its easy to get the column names present in the first row by their index(csv[0]), but there is no easy way to get the count of columns. – B A Jan 17 '18 at 18:21
  • Still simpler to create a class - even with 50 props - than to read and parse and guestimate data type – Ňɏssa Pøngjǣrdenlarp Jan 17 '18 at 18:21
  • Have you tried `csv.ReadHeader()`? – Isaac Kleinman Jan 17 '18 at 18:22
  • @IsaacKleinman `ReadHeader()` is just the same. No way to get the total count. – B A Jan 17 '18 at 18:22
  • If you are intent on doing it that way you probably want to use the CSVParser approach not the CSVReader – Ňɏssa Pøngjǣrdenlarp Jan 17 '18 at 18:24
  • @Plutonix actually I think I will be playing around with multiple csv files so adding the properties all the time would take up a lot of time :) Could you please give a link to the documentation for the CSVParser? I couldn't find it. – B A Jan 17 '18 at 18:29
  • Possible duplicate of [How to read a header from a specific line with CsvHelper?](https://stackoverflow.com/questions/39641220/how-to-read-a-header-from-a-specific-line-with-csvhelper) – derloopkat Jan 17 '18 at 18:37
  • @derloopkat nope thats a totally different question. – B A Jan 17 '18 at 18:38

2 Answers2

58

The header record is on the csv context. It needs to be read beforehand before accessing.

csv.Read();
csv.ReadHeader();
string[] headerRow = csv.Context.HeaderRecord;
Paul Wild
  • 608
  • 6
  • 10
7

The previous answer used to work fine, but as of version 20 of csvhelper there is a breaking change. Now just access the header record from the reader directly:

csv.Read();
csv.ReadHeader();
string[] headerRow = csv.HeaderRecord;
JumpingJezza
  • 5,498
  • 11
  • 67
  • 106
  • 1
    NOTE: These are the unmunged headers. If you use `PrepareHeaderForMatch` to munge the headers (replace whitespace with underscores, for instance) that won't be reflected in the `HeaderRecord` list. – amonroejj Mar 17 '21 at 16:31