JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.65k stars 1.05k forks source link

setting HasHeaderRecord on Context.Configuration has no effect #2159

Open jeoffm opened 1 year ago

jeoffm commented 1 year ago

Describe the bug the class model allows this _csvWriter.Context.Configuration.HasHeaderRecord = false; but it is not honored as the code always refers to the private member private readonly bool hasHeaderRecord; which can only be set at instantiation time

Expected behavior the Context's value should be honored, otherwise why have a context??

AltruCoder commented 1 year ago

There was a breaking change with CsvHelper 20.0.0

20.0.0 Features Changed CsvConfiguration to a read only record to eliminate threading issues.

You need to pass a CsvConfiguration to CsvWriter

void Main()
{
    var records = new List<Foo>
    {
        new Foo { Id = 1, Name = "one" },
    };

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        HasHeaderRecord = false
    };

    //using (var writer = new StreamWriter("path\\to\\file.csv"))
    using (var csv = new CsvWriter(Console.Out, config))
    {
        csv.WriteRecords(records);
    }
}

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}