CsvHelper – Cannot write to a closed TextWriter.

If you are using CsvHelper and get the error “Cannot write to a closed TextWriter”, here is a solution.

You are trying to reuse the same underlying stream to write using CsvWriter

using (var csv = new CsvWriter(writer))
{
	// your code here
}

Instead, you don’t want to close the underlying stream so change your code to

using (var csv = new CsvWriter(writer, true))
{
	// your code here
}

“true” parameter will leave the underlying stream open.

Share

0 comments

Your email address will not be published. Required fields are marked *