Search code examples
c#.netcsvhelper

CsvHelper The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)


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

namespace CsvHelperTest
{
    internal class CsvHelperTester
    {
        static void Main(string[] args)
        {
             
            using (var streamReader = new StreamReader("C:\\Users\\eyoung\\Desktop\\parse test files\\SWR-150106-1_AN00045613_20150415_XF28572.csv"))
            {
                using (var csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture))
                {

                    var csvReaderConfig = new Configuration();

                    csvReaderConfig.HasHeaderRecord = false;
                    var records = csvReader.GetRecords<dynamic>().ToList();
                }
            }
        }
    }
}

Initially my error without

var csvReaderConfig = new Configuration();

csvReaderConfig.HasHeaderRecord = false;

was ArgumentException: An element with the same key '' already exists in the ExpandoObject. (Parameter 'key')

and I was told adding the code above would solve this issue, however, it has not, not sure if I'm missing something in the documentation or if it's something that isn't installed, any help would be appreciated, thanks!


Solution

  • You need to use CsvConfiguration and it needs to be passed into the CsvReader constructor. You can find examples of using CsvConfiguration on the Getting Started Page

    static void Main(string[] args)
    {
        var csvReaderConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            HasHeaderRecord = false
        };
    
        using (var streamReader = new StreamReader("C:\\Users\\eyoung\\Desktop\\parse test files\\SWR-150106-1_AN00045613_20150415_XF28572.csv"))
        {
            using (var csvReader = new CsvReader(streamReader, csvReaderConfig))
            {
                var records = csvReader.GetRecords<dynamic>().ToList();
            }
        }
    }