1

I'm using LinqToCSV to write a list out to a csv file like this:

var outputFile = ConfigurationManager.AppSettings["OutputFile"];
var context = new CsvContext();            
context.Write(cardholders, outputFile, outputDescription);

However, whenever this runs, it will overwrite the existing output file. How do I get it to create a new file (if it's not there) or append (if it exists)

Null Reference
  • 11,260
  • 40
  • 107
  • 184

1 Answers1

5

Instead of passing an output file path, pass instead an overload of a TextWriter which will create a new file if one doesn't exist and append content to one that does:

using (TextWriter writer = new StreamWriter(ConfigurationManager.AppSettings["OutputFile"], true)) {
    var context = new CsvContext();
    context.Write(cardholders, writer, outputDescription);
} 
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151