Replace file with another in atomic operation (Windows)

I have two files: A and B. I need to replace B with A, and B in another program.

The following approach:

MoveFile to rename B to C, then rename A to B and then delete C

works great. However, I need an atomic way to do this, because if one renaming fails, the data will be left in an inconsistent state.

I tried MoveFileEx with the MOVEFILE_REPLACE_EXISTING flag, but it does not work when file B is opened by another program. Same thing for the ReplaceFile function. Any other ideas? thank you

+4
source share

Source: https://habr.com/ru/post/1479997/


All Articles