how to append data using csvHelper in c# code example

Example: how to append data using csvHelper in c#

using CsvHelper;

public void WriteDataToCsv(MsgEnvironmentData[] data, string csvPath)
{
           
       if (!File.Exists(csvPath))
       {
         using (var writer = new StreamWriter(csvPath))
         using (var csvWriter = new CsvWriter(writer,firstConfiguration))
          {

              csvWriter.WriteHeader<MsgEnvironmentData>();
              csvWriter.NextRecord();
              csvWriter.WriteRecords(data);
          }
       }
       else
       {
           using (var stream = new FileStream(csvPath, FileMode.Append))
           using (var writer = new StreamWriter(stream))
           using (var csvWriter = new CsvWriter(writer, secondConfiguration))
            {
                csvWriter.WriteRecords(data);
            }
        }
}

Tags:

Misc Example