移动文件() 不起作用

MoveFile() doesn't work

本文关键字:不起作用 文件 移动      更新时间:2023-10-16

我正试图将一些文件从一个目录移动到同一驱动器上的另一个目录。所有目标目录都存在,但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, "");
}

使用MoveFile而不是CopyFile和不使用MoveFileEx的原因。根据微软的MSDN文档,它逐字声明,当您使用MoveFile时,它不会复制安全描述,并且您将丢失到新位置的默认值,这是旁注。

但是由于它返回0意味着失败,我们可以通过调用GetLastError API并查找结果数字到适当的错误字符串来扩展该错误,因此它更有意义。除了像这样在黑暗中随意开枪。此外,这个项目是否使用Unicode/Ansi或Not-Set作为默认字符集?

而且,在没有给我们一个回归测试的情况下发布整个问题,这样我们就可以评估它并告诉你哪里出了问题,这不是很有帮助。你想让别人快速解决你的问题,同时这对你和其他人来说也是一个学习的地方。