25

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

What does "all intermediate file buffers" mean exactly?

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
cooldfish
  • 327
  • 1
  • 3
  • 5
  • 1
    Note Flush(true) is broken under certain conditions per MS bug report http://connect.microsoft.com/VisualStudio/feedback/details/634385/filestream-flush-flushtodisk-true-call-does-not-flush-the-buffers-to-disk#details. MS fixed it, but won't say what .NET version it is fixed in. – jimvfr Feb 15 '13 at 21:58
  • 1
    Any word on what version of .NET this bug is fixed in ? – BaltoStar Feb 06 '14 at 22:52

3 Answers3

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.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 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 09 '18 at 00: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.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • There is no "encoder state" in `FileStream`, it takes bytes - not characters - and transfers them from/to disk. Were you perhaps thinking of `StreamWriter`? – Evgeniy Berezovsky Jun 20 '17 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 '18 at 16:42
1

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

 Win32Native.FlushFileBuffers(this._handle);
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 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) – R. Martinho Fernandes Feb 07 '11 at 13:00
  • 1
    I used reflector to reverse engineer. Theer is nothing illegal with that. – Aliostad Feb 07 '11 at 13:01
  • 1
    Posting source code - This has been discussed a lot on Meta. For example http://meta.stackexchange.com/questions/74890/is-it-inappropriate-to-post-decompiled-code-from-the-net-bcl and http://meta.stackexchange.com/questions/20153/posting-code-from-reflector – bic Feb 07 '11 at 13:57
  • 2
    @Aliostad: that you can read the code legally does not mean it's legal to publish it here. – Fredrik Mörk Feb 07 '11 at 14:03
  • OK guys I have removed it. But without looking into the source code, how else can you **reliably** say what it does? I just wanted to make it clear this has been looked up from the original code. – Aliostad Feb 07 '11 at 14:06
  • 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 http://www.microsoft.com/resources/msdn/en-us/MSDN-FILES/027/002/097/ShSourceCLILicense.htm. "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 07 '11 at 14:19
  • 1
    The consensus seems to be that there's nothing *legally* wrong with posting the disassembled code from Reflector, but several people who work on projects like Mono have asked that you do not so. They're bound by an agreement that they not look at MS's code. Having seen it could compromise their ability to continue work on the project. – Cody Gray - on strike Feb 07 '11 at 15:16