Write to a File using CsvHelper in C#

You need to flush the stream. The Using statement will flush when out of scope.

using (TextWriter writer = new StreamWriter(@"C:\test.csv", false, System.Text.Encoding.UTF8))
{
    var csv = new CsvWriter(writer);
    csv.WriteRecords(values); // where values implements IEnumerable
}

when, I added this code after the loop code is working well

var csv = new CsvWriter(writer);
csv.Configuration.Encoding = Encoding.UTF8;
foreach (var value in valuess)
{
     csv.WriteRecord(value);
}
writer.Close();

The problem occurred because I did not close the Connection


Assuming that writer is some kind of TextWriter, you should add a call to flush the contents before closing the writer:

writer.Flush()

If the last lines are missing, this is the most likely reason.

Tags:

C#

Csvhelper