Csvhelper: Problem writing out records.

Created on 16 Aug 2012  路  9Comments  路  Source: JoshClose/CsvHelper

I have created a simple VS2010 MVC 3 c# solution located at https://www.dropbox.com/s/45veyjnoqy9to9i/CSVHelperTest.zip and would really appreciate it if you could have a quick look.

There is a csv file included (Book1.csv) which you place in c:\ and then when running the app go to localhost:xxxx/Home/Process

You can view the process action in the HomeController. The process creates a new.csv file in c:\ and simply writes out the records that it reads in from Book1.csv The result though is a blank file? the process action also returns you to a view that outputs the loaded records to screen.

Thanks,
Steve.

Most helpful comment

The reason is the TextWriter is never flushed. I removed the auto-flushing that used to occur after each record write, because it caused a large slow down when writing over a network. If you dispose of your CsvWriter or StreamWriter, the writer will get flushed. You can also explicitly call TextWriter.Flush(). If you have a StreamWriter, which you do in your case, you can turn on StreamWriter.AutoFlush = true, and that will flush automatically on every write for you. The TextWriter will flush on it's own when it's full or when it's disposed of.

As a best practice, I always put disposables in a using block:

using( var writer = new StreamWriter( "c:\\new.csv" ) )
using( var csvWriter = new CsvWriter( writer ) )
{
    // You can turn AutoFlush on and handle it that way,
    // but it will flush on every write this way, which might not be ideal.
    //writer.AutoFlush = true;

    // You can change configuration here.
    writer.Configuration.QuoteAllFields = true;

    writer.WriteRecords( dataList );

    // You can manually flush.
    //writer.Flush();
} // The best solution is to let it flush on dispose after the using block exits here.

I mentioned this in the notes of the 1.6 release also, but it's pretty easy to miss. Especially if you're just installing via NuGet from Visual Studio.

https://nuget.org/packages/CsvHelper/1.6.0

All 9 comments

The reason is the TextWriter is never flushed. I removed the auto-flushing that used to occur after each record write, because it caused a large slow down when writing over a network. If you dispose of your CsvWriter or StreamWriter, the writer will get flushed. You can also explicitly call TextWriter.Flush(). If you have a StreamWriter, which you do in your case, you can turn on StreamWriter.AutoFlush = true, and that will flush automatically on every write for you. The TextWriter will flush on it's own when it's full or when it's disposed of.

As a best practice, I always put disposables in a using block:

using( var writer = new StreamWriter( "c:\\new.csv" ) )
using( var csvWriter = new CsvWriter( writer ) )
{
    // You can turn AutoFlush on and handle it that way,
    // but it will flush on every write this way, which might not be ideal.
    //writer.AutoFlush = true;

    // You can change configuration here.
    writer.Configuration.QuoteAllFields = true;

    writer.WriteRecords( dataList );

    // You can manually flush.
    //writer.Flush();
} // The best solution is to let it flush on dispose after the using block exits here.

I mentioned this in the notes of the 1.6 release also, but it's pretty easy to miss. Especially if you're just installing via NuGet from Visual Studio.

https://nuget.org/packages/CsvHelper/1.6.0

You're a star. Thanks Josh.

No problem. :)

Works a treat. Thanks. :)

Good to hear!

That helped so much. Thanks

Fixed my problem. Thanks!

Thanks, my problem is also fixed

The reason is the TextWriter is never flushed. I removed the auto-flushing that used to occur after each record write, because it caused a large slow down when writing over a network. If you dispose of your CsvWriter or StreamWriter, the writer will get flushed. You can also explicitly call TextWriter.Flush(). If you have a StreamWriter, which you do in your case, you can turn on StreamWriter.AutoFlush = true, and that will flush automatically on every write for you. The TextWriter will flush on it's own when it's full or when it's disposed of.

As a best practice, I always put disposables in a using block:

using( var writer = new StreamWriter( "c:\\new.csv" ) )
using( var csvWriter = new CsvWriter( writer ) )
{
    // You can turn AutoFlush on and handle it that way,
    // but it will flush on every write this way, which might not be ideal.
    //writer.AutoFlush = true;

    // You can change configuration here.
    writer.Configuration.QuoteAllFields = true;

    writer.WriteRecords( dataList );

    // You can manually flush.
    //writer.Flush();
} // The best solution is to let it flush on dispose after the using block exits here.

I mentioned this in the notes of the 1.6 release also, but it's pretty easy to miss. Especially if you're just installing via NuGet from Visual Studio.

https://nuget.org/packages/CsvHelper/1.6.0

This was not clear in the documentation but does make sense.

Was this page helpful?
0 / 5 - 0 ratings