How to avoid file corruption upon power shortage

corruptionwindows 7

I have an industrial PC (Windows posready 7) running a server application that was corrupting files too often when the pc was abruptly turned off (power shortage) and I found that changing my code to set the FILE_WRITE_THROUGH flag solved the problem when the file was written by my application.

The problem is that when I copy+paste some file in the system, wait 5 seconds, then power off (unplug) the machine, the files are still always getting corrupted, by opening the corrupted files in an hex editor, I can see that from half of the file on, all its bytes were set to 0.

Is there a way to prevent this effect? Is it possible to change the default value for FILE_WRITE_THROUGH at system level so that all file operations start behaving like my application?

Best Answer

Apart from installing a UPS (which you really should), there is a setting you can use to tell the system to make sure everything is immediately written to disk instead of being kept in the system cache:

Disable write caching

Of course, disabling disk caching will reduce system performance. But f.e. it's automatically done by Windows on domain controllers, which absolutely must make a sure a directory update is actually written to the AD database when a DC thinks it is.

Related Topic