Save File Safely in Windows 10 IOT

My team requires a bulletproof way to save a file (less than 100KB) on Windows 10 IOT.

The file cannot be corrupted, but it is ok to lose the most recent version if the save fails due to a power outage, etc.

Since File IO has changed significantly (no larger than File.Replace), we're not sure how to achieve it.

We can see that:

var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
await Windows.Storage.FileIO.WriteTextAsync(file, data);

      

reliably unreliable (it got interrupted multiple times when debugging or resetting the device.) and we end up with a corrupted file (full of zeros) and a .tmp file next to it. We can restore this .tmp file. I'm not sure if we should base our decision on undocumented behavior.

One way we want to try:

var tmpfile = await folder.CreateFileAsync(fileName+".tmp",
                               CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteTextAsync(tmpfile, data);

var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

// can this end up with a corrupt or missing file?
await tmpfile.MoveAndReplaceAsync(file); 

      

All in all, is there a safe way to save some text in a file that will never damage the file?

+3


source to share


1 answer


Not sure if there is a best practice for this, but if you need to come up with something yourself:

I would do something like calculating the checksum and save it along with the file.



When saving next time, do not overwrite it, but save it next to the previous one (which should be "well known") and delete the previous one only after checking the successful completion of the new save (along with the checksum)

Also I would suggest that the rename operation shouldn't damage the file, but I haven't investigated that

0


source







All Articles