问题
I am having a problem moving a directory to a new location, specifically one I create with CreateDirectory
. Here is the code:
if (FALSE == CreateDirectory(strDestination, NULL))
{
dwError = GetLastError();
if (ERROR_ALREADY_EXISTS != dwError)
{
strError.Format("Error creating %s: %i", strDestination, dwError);
LogIt(strError);
}
}
if (FALSE == MoveFile(strSource, strDestination + strID))
{
dwError = GetLastError();
strError.Format("Error moving %s to %s: %i", strSource, strDestination + strID, dwError);
LogIt(strError);
}
However, if I manually create a directory, I am able to feed that path into this code and it works. I have compared the security settings for these two directories, and made sure they were the same, but it's still not working. Is there something I'm doing wrong with my creation code? If I leave the second parameter as NULL
, shouldn't it grant the same permissions it would when I manually create the directory?
回答1:
If you're running antivirus on the machine, it could be locking the new folder while it verifies it/adds it to its clean cache. That could cause intermittent timing issues if, for example, the AV driver is bogged down with other activity.
To check that this is what's happening you could disable your AV's on-access scanner. A workaround in your code would be to retry (say) 2-3 times with a small delay between.
Edit: Since the OP has confirmed that it's failing moving to a different volume, the answer is to use MoveFileEx() with the MOVEFILE_COPY_ALLOWED flag.
来源:https://stackoverflow.com/questions/13352396/c-movefile-giving-error-access-denied-inconsistently-for-directory-moves-on-wi