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.
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.
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
TextWriteris 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 yourCsvWriterorStreamWriter, the writer will get flushed. You can also explicitly callTextWriter.Flush(). If you have aStreamWriter, which you do in your case, you can turn onStreamWriter.AutoFlush = true, and that will flush automatically on every write for you. TheTextWriterwill 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.
This was not clear in the documentation but does make sense.
Most helpful comment
The reason is the
TextWriteris 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 yourCsvWriterorStreamWriter, the writer will get flushed. You can also explicitly callTextWriter.Flush(). If you have aStreamWriter, which you do in your case, you can turn onStreamWriter.AutoFlush = true, and that will flush automatically on every write for you. TheTextWriterwill 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:
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