Send the contents of a datatable to a csv file with headers
<p>I am trying to send the contents of a datatable to a csv file, with headers. There is a duplicate question but the the accepted answers only seem to work half way. At this point I have mixed and matched the upvoted answers with no luck and need a point in the right direction. </p>

<p>I can write the columns to the file just fine, and I can write data just fine but not together. Also the data never comes out quoted, only comma delimited without quotes.</p>

<pre><code>//This is how the FileHelpers class is built
public class ScannedFileInfo
{
    //this prefix will handle the double quotes issue
    //delimiters must reside between double quotes
    //Must specify FieldOrder too
    [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
    [FieldOrder(1)]
    public string DA_Id;
    [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
    [FieldOrder(2)]
    public string Name;
    [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
    [FieldOrder(3)]
    public string Extension;
    [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
    [FieldOrder(4)]
    public string Fullname;
    [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
    [FieldOrder(5)]
    public string PathLength;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    [FieldOrder(6)]
    public string Directory;
}

//this is how I send it to the file
public static void ImportDirectory(string result, SqlConnection myconn, SqlConnection destConn ,ListBox lbInfo, DataGridView dgView)
{
    //create data table code here - works fine...
    MessageBox.Show("Scan complete. Starting Import...");

    //build file 
    var engine = new FileHelperEngine&lt;ScannedFileInfo&gt;();
    var orders = new List&lt;ScannedFileInfo&gt;();
    engine.HeaderText = engine.GetFileHeader();
    engine.WriteFile(@"C:\DirectoryScan.csv", orders);
    MessageBox.Show("You now have proper labeled columns in your file.");

    //now the data import is successful but it overwrites the column info
    CommonEngine.DataTableToCsv(dt, @"C:\DirectoryScan.csv", ',');
    MessageBox.Show("You now have data in your file, but no columns");

}
</code></pre>