How to ensure all data has been physically written to disk?

后端 未结 7 1107
执念已碎
执念已碎 2020-11-27 05:44

I understand that .NET FileStream\'s Flush method only writes the current buffer to disk, but dependent on Windows\' disk driver and the hard disk firmware this is no guaran

相关标签:
7条回答
  • 2020-11-27 06:05

    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.

    0 讨论(0)
  • 2020-11-27 06:08

    There's simply too many levels of abstraction to be absolutely sure that the data is written to the disc, right down to the hardware level.

    Not brilliantly performant or foolproof, but how about re-opening the file once it is written in a seperate process and checking the size or contents?

    0 讨论(0)
  • 2020-11-27 06:09

    Well, you could close the file... that would probably do it. In reality, with HAL abstraction, virtualization, and disk hardware now having more processing power and cache memory than computers did a few years ago, you're going to have to live with hoping the disk does its job.

    The transactional file system never really materialized ;-p Of course, you could perhaps look at using a database as a back end, and use the transaction system of that?

    Aside: note that not all streams even guarantee to Flush() - for example, GZipStream etc retain a working buffer of uncommitted data even after a flush - the only way to get it to flush everything is to Close() it.

    0 讨论(0)
  • 2020-11-27 06:13

    Under Windows, look at FlushFileBuffers (Win32 API).

    0 讨论(0)
  • 2020-11-27 06:21

    I've noticed that the .NET 4 #Flush(true) doesn't actually write to the disk. We were having strange issues with corrupted data and I found this bug report on the MS site:

    The details tab for the bug report has a test program you can run that will show the issue;

    1. Write a bunch of data to disk
    2. fs.Flush(true). This takes no time (much faster than can possibly written to the disk).
    3. Use the win32 API FlushFileBuffers. This takes a long time.

    I'm changing over to the win32 FlushFileBuffers call...

    0 讨论(0)
  • 2020-11-27 06:23

    There is a simple answer to flushing the content of the buffer to disk. After your WriteAllText function, open file, close it, and reset it

    here is an example

    My.Computer.FileSystem.WriteAllText(yourfilename, "hello", False, System.Text.Encoding.ASCII)
    FileOpen(1, yourfilename, OpenMode.Input)
    FileClose(1)
    Reset()
    
    0 讨论(0)
提交回复
热议问题