刷新磁盘写缓存 [英] Flush disk write cache
问题描述
在Windows XP和Vista中将磁盘策略设置为启用硬盘上的写缓存时,是否可以刷新刚写入的文件并确保已将其提交到磁盘?
我想以C ++编程方式完成此操作。
关闭文件确实会在应用程序级别执行刷新操作,但不在操作系统级别。如果在关闭文件后但在操作系统刷新磁盘写缓存之前从PC上断开电源,即使文件已关闭,该文件也会丢失。 >解决方案
您尚未指定开发环境,所以:
.Net
IO流具有 .Flush
方法,可以满足您的需求。
Win32 API
有 FlushFileBuffers
调用,该调用将文件句柄作为参数。
EDIT(基于OA的评论):FlushFileBuffers不需要管理特权;仅当传递给它的句柄是卷的句柄(而不是单个文件的句柄)时才起作用。
When the policy for a disk in Windows XP and Vista is set to enable write caching on the hard disk, is there a way to flush a file that has just been written, and ensure that it has been committed to disk?
I want to do this programmatically in C++.
Closing the file does perform a flush at the application level, but not at the operating system level. If the power is removed from the PC after closing the file, but before the operating system has flushed the disk write cache, the file is lost, even though it was closed.
You haven't specified the development environment, so:
.Net
IO streams have a .Flush
method that does what you want.
Win32 API
There is the FlushFileBuffers
call, which takes a file handle as argument.
EDIT (based on a comment from the OA): FlushFileBuffers does not need administrative privileges; it does only if the handle passed to it is the handle for a volume, not for a single file.
这篇关于刷新磁盘写缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!