CsvHelper – Cannot write to a closed TextWriter.

By | December 16, 2019 | 0 Comment
Category: Tricks n Techniques Tags:

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


About Vishal Monpara

Vishal Monpara is a full stack Solution Developer/Architect with 13 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1's and 0's from geographically dispersed hard disks to geographically dispersed user leveraging geographically dispersed team members.