What's the difference between FileStream.Flush() and FileStream.Flush(True)?

北战南征 提交于 2019-12-21 06:50:27

问题


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

What does "all intermediate file buffers" mean exactly?


回答1:


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.




回答2:


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.




回答3:


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

 Win32Native.FlushFileBuffers(this._handle);


来源:https://stackoverflow.com/questions/4921498/whats-the-difference-between-filestream-flush-and-filestream-flushtrue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!