我正在尝试将一些文件从一个目录移动到同一驱动器上的另一个目录中。所有目标目录都存在,但MoveFile()不起作用,GetLastError()返回0。我是C++的新手,我不知道是什么导致了这个问题。代码如下:
try
{
destinationFilePath = i->FilePath;
destinationFilePath=destinationFilePath.substr(destinationFilePath.find(GENERAL_SAVE_PATH) + strlen(GENERAL_SAVE_PATH.c_str()));
destinationFilePath = Backup_Save_Path + destinationFilePath;
vector<string> folders;
StringSplit(destinationFilePath,"\\",folders);
string pathToCreate;
string backSlash = "\\";
for(vector<string>::const_iterator j = folders.begin(); j != folders.end(); ++j)
{
pathToCreate += j->c_str() + backSlash;
if (pathToCreate.find(".txt") == std::string::npos)
CreateBackupFolders(pathToCreate);
}
if (MoveFile(lastExportedFilePath.c_str(),destinationFilePath.c_str()))
AfxMessageBox("moved");
else
{
DWORD dw = GetLastError();
AfxMessageBox("Couldn't move");
}
lastExportedFilePath = i->FilePath;
}
catch(...)
{
Log(LOG_PATH_LOG_INSERTING, LOG_TYPE_EXCEPTION, "ExportLogsToDB()", "Move", destinationFilePath, "");
}发布于 2015-08-30 17:55:03
关于在CopyFile上使用MoveFile和不使用MoveFileEx的原因。根据微软的MSDN文档,它原封不动地声明,当你使用MoveFile时,它不会复制安全描述,并且你将输给新位置上的任何默认设置,这是附注。
但由于它返回0表示失败,我们可以通过调用GetLastError API并查找相应的错误字符串来扩展该错误,因此它更有意义。而不是像这样在黑暗中随意射击。另外,这个项目是使用Unicode/Ansi还是Not-Set作为默认字符集?
此外,发布整个问题而不给我们一个回归测试,这样我们就可以评估它并告诉你哪里出了问题,这是没有太大帮助的。你正在遵循一条线,在这里你希望有人快速解决你的问题并解决它,而这对你和其他人来说都是一个学习的地方。
https://stackoverflow.com/questions/32294389
复制相似问题