I'm using File.WriteAllBytes to write changes to a file.
If a power outage happens after calling WriteAllBytes, the file always becomes corrupted. The problem is that this happens not only during the execution of my Save function but even after the execution of the function has finished - sometimes even after an hour after i called Save.
I tried using BinaryWriter but i got the same results. I also tried implementing a workaround by using temp files. The problem is that File.Copy also has the same behavior - i.e. if i create a temp file, and a power outage happens sometime during runtime, the file will become corrupted.
In contrast i noticed that even if i kill my application from the task manager after writing to a file, the file does not become corrupted even if this happens seconds after i call Write.
Do you happen to know why this happens and perhaps suggest a workaround?
According to documentation, WriteAllBytes
Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
The assumption of course is that since the file is both written and closed by WriteAllBytes
, then it should be reliably on disk immediately after this method returns. But that is a faulty assumption due to filesystem caching policies that may be in force. Furthermore, even when a filesystem flushes its cache to disk (and in "write-through" filesystems) and the hardware has returned a "success" indication, there is a delay (albeit a very short delay) caused by the hardware cache before the data is actually physically written to disk.
If your data is so important that it must survive a power outage, be sure to have an UPS installed.
All that being said, having waited an hour to pull the plug and still seeing your file corrupted sounds suspect, like maybe it's not due to filelsystem caching. There could be a bug in your logic, or an internal or external resource conflict.
I would try a few things.
WriteAllBytes
returns, or as soon thereafter as you reasonably can (preferably not inside of your application, but in another applciation running beside it).WriteAllBytes
and hangs around and pull the plug while that's running. Does that file get corrupted?Lastly, WriteAllBytes
is a widely used method that isn't very complicated. I doubt that it is causing your problem.