CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

setting HasHeaderRecord on Context.Configuration has no effect

Open jeoffm opened this issue 2 years ago • 1 comments

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??

jeoffm avatar May 14 '23 20:05 jeoffm

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; }
}

AltruCoder avatar May 15 '23 16:05 AltruCoder