Reputation: 16724
I'm writing a lot of data to a file it may take a long time so I want to write the contents to file as soon I call my save()
function. Currently the file size (I'm watching from Windows Explorer) stay at 0 bytes size until program writing terminates.
I tried both:
fileOut = new StreamWriter(fileName);
fileOut.AutoFlush = true;
and
fileOut.Write(contents);
fileOut.Flush();
How do I fix this?
Upvotes: 2
Views: 298
Reputation: 171246
This is a typical symptom of not closing the stream. Wrap your resource usages in a using statement, like always.
Flushing does not do what you think it does. It does not make data visible to other programs. It forces it do disk. This is not your problem.
It doesn't matter what's on disk. What matters is what the Windows File Cache presents to other programs. File contents can be in memory and still be synchronized between applications.
The metadata behavior (length, times, ...) is strange on Windows. Turns out that enumerating a directory can return stale data. Directly looking at a file (opening its Properties Window) should return accurate information at all times.
The linked Raymond Chen article has a workaround you can use to force current information to be made visible immediately.
Upvotes: 4