FileStream.Flush() 和 FileStream.Flush(True) 有什么区别?

发布于 2024-10-16 03:19:08 字数 302 浏览 6 评论 0原文

MSDNFileStream.Flush(True) "也清除所有中间文件缓冲区。”。

“所有中间文件缓冲区”到底是什么意思?

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

What does "all intermediate file buffers" mean exactly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杯别 2024-10-23 03:19:09

当您调用 Flush()Flush(false) 时,FileStream 会将之前写入缓冲区的任何数据复制到文件并清除缓冲区(其编码器状态除外)”。这里的Buffer是指FileStream类的内部缓冲区。复制到文件并不是将数据写入光盘。它只是将数据传递给操作系统。

但是,Windows 操作系统中的 IO 操作也会被缓冲 - 将数据写入磁盘可能会被推迟,直到系统准备好执行此操作。因此,清除所有中间缓冲区会强制将缓冲数据写入磁盘。这里的Buffers是指Windows内部缓冲区【文件系统缓存】。

顺便说一句,当您关闭文件时,所有缓冲的数据将自动写入光盘。因此,只有当您需要在文件句柄关闭之前刷新数据时,您才需要这些东西。

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.

煮茶煮酒煮时光 2024-10-23 03:19:09

这将进行额外的调用以将缓冲区刷新到文件:

 Win32Native.FlushFileBuffers(this._handle);

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

 Win32Native.FlushFileBuffers(this._handle);
ぃ弥猫深巷。 2024-10-23 03:19:08

它导致文件系统缓存中缓冲的文件数据写入磁盘。该数据通常是根据磁盘写入头的位置延迟写入的。拥有千兆字节的缓存数据在技术上是可行的,因此可能需要相当长的时间。如果这对您很重要,那么请考虑使用 FileOptions.WriteThrough 选项。它完全禁用写缓存。这可能会非常昂贵;你会发现硬盘到底有多慢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文