25

MSDN says that FileStream.Flush(True) "also clears all intermediate file buffers.".

What does "all intermediate file buffers" mean exactly?

2

3 Answers 3

27

It causes the file data that's buffered in the file system cache to be written to disk. That data is normally lazily written, based on the position of the disk write head. Having a gigabyte of cached data is technically possible so it can take quite a while. If this is important to you then consider the FileOptions.WriteThrough option instead. It disables write caching completely. This can be very expensive; you'll discover how slow hard disks really are.

1
  • Keep in mind that FileStream itself has a buffer. So using FileOptions.WriteThrough is not an alternative to calling FileStream.Flush(True) unless you also set BufferSize to 1.
    – Sam Porch
    Jul 9, 2018 at 0:56
11

When you call Flush() or Flush(false), FileStream "copies to the file any data previously written to the buffer and clears the buffer (except for its encoder state)". Buffer here means internal buffer of FileStream class. And copying to file is not writing data to disc. It's just passing data to OS.

But, IO operations in Windows OS are also buffered - writing data to disk could be postponed until system will be ready to do it. So, clearing all intermediate buffers enforces writing buffered data to disc. Buffers here means Windows internal buffers [File system cache].

BTW when you close file, all buffered data will be written to disc automatically. So, you need this stuff only if you need data to be flushed before file handle will be closed.

2
  • There is no "encoder state" in FileStream, it takes bytes - not characters - and transfers them from/to disk. Were you perhaps thinking of StreamWriter? Jun 20, 2017 at 23:59
  • 1
    It appears that when closing the file, all buffered data is written to the OS rather than to disk. Or so performance tests indicate: File.AppendAllText() has a duration the same order of magnitude as fileStream.Write(); fileStream.Flush(false);, whereas fileStream.Write(); fileStream.Flush(true); is an order of magnitude slower. Since File.AppendAllText() immediately closes the file again, and takes nowhere near as long as fileStream.Flush(true), we can assume that closing the file writes the buffered data to the OS, rather than forcing the OS to write it to disk.
    – Timo
    Nov 20, 2018 at 16:42
1

This will make an extra call to flush the buffer to file:

 Win32Native.FlushFileBuffers(this._handle);
7
  • 2
    Is it ok to post source code from the MS implementation? (despite the fact that I think the whole code here doesn't add value to the answer) Feb 7, 2011 at 13:00
  • 1
    I used reflector to reverse engineer. Theer is nothing illegal with that.
    – Aliostad
    Feb 7, 2011 at 13:01
  • 1
    Posting source code - This has been discussed a lot on Meta. For example meta.stackexchange.com/questions/74890/… and meta.stackexchange.com/questions/20153/…
    – bic
    Feb 7, 2011 at 13:57
  • 2
    @Aliostad: that you can read the code legally does not mean it's legal to publish it here. Feb 7, 2011 at 14:03
  • 5
    This question hasn't been answered definitively as far as I can see. The MS shared source license does allow distribution for educational purposes microsoft.com/resources/msdn/en-us/MSDN-FILES/027/002/097/…. "You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software."
    – bic
    Feb 7, 2011 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.