Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET File Stream write method writes all empty lines

I am having a C# console application access files over a network and write to it. I noticed that some files have been corrupted and have only null written to them. I did not get any exceptions. I am using simple code that writes a byte array to the file's stream. When opening the files in Binary mode, all i see are Zeros, something like "0: 00 00 00 00 10: 00 00 00 00".

Does anyone know why such a thing would happen? There could have been a network failure, but network failures should have thrown some IO exceptions right?

Let me know if anyone has any idea about this.

Code sample:

FileInfo fi = new FileInfo(filePath);
using (FileStream fs = fi.Open (FileMode.Create, FileAccess.Write, FileShare.None))
{
   fs.Write(byteData, 0, byteData.Length);
}
like image 977
Srijanani Avatar asked Nov 17 '25 20:11

Srijanani


2 Answers

Make sure to call FileStream.Flush() where appropriate.

like image 149
Andy West Avatar answered Nov 20 '25 12:11

Andy West


Make sure you're closing the file.

like image 37
tzerb Avatar answered Nov 20 '25 10:11

tzerb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!