C++ MoveFile giving ERROR_ACCESS_DENIED inconsistently for directory moves on Windows Server

醉酒当歌 提交于 2021-01-29 13:24:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!