This comes from Windows, different error codes for different scenarios. The second option is ERROR_ALREADY_EXISTS, "It is impossible to create a file if this file already exists" when the file is moved from one directory to another on the same drive. This is a fairly simple operation, you only need to move the entry in the directory.
The first one is ERROR_FILE_EXISTS, "File exists" when a file is moved from one drive to another. This is a much more complicated operation; file data must also be copied. In other words, it returns to the equivalent of File.Copy (string, string, bool) with the last rewrite argument set to false. That he does not use the same error code is a bit of a quirk. The difference is largely due to the file system driver, not your program. Otherwise, the reason why you just get a rather typical IOException, and not a more specific one, which destroys file manipulation errors into smaller exceptions.
Actually, this is not a problem, because there is nothing to be done in your code, you need the help of a person to fix the problem. If you are not taking specific preventive measures in your own code, avoid moving if the target file already exists or actually deletes the target file in the first place. Please note that none of them is a 100% reliable workaround, there is a very small chance that another process will create the file again immediately after it is deleted, but before it is moved. Making file operations completely reliable is quite difficult in a multitasking operating system.
source share