首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MoveFile()不起作用

MoveFile()不起作用
EN

Stack Overflow用户
提问于 2015-08-30 15:47:05
回答 1查看 1.4K关注 0票数 1

我正在尝试将一些文件从一个目录移动到同一驱动器上的另一个目录中。所有目标目录都存在,但MoveFile()不起作用,GetLastError()返回0。我是C++的新手,我不知道是什么导致了这个问题。代码如下:

代码语言:javascript
复制
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, "");
}
EN

回答 1

Stack Overflow用户

发布于 2015-08-30 17:55:03

关于在CopyFile上使用MoveFile和不使用MoveFileEx的原因。根据微软的MSDN文档,它原封不动地声明,当你使用MoveFile时,它不会复制安全描述,并且你将输给新位置上的任何默认设置,这是附注。

但由于它返回0表示失败,我们可以通过调用GetLastError API并查找相应的错误字符串来扩展该错误,因此它更有意义。而不是像这样在黑暗中随意射击。另外,这个项目是使用Unicode/Ansi还是Not-Set作为默认字符集?

此外,发布整个问题而不给我们一个回归测试,这样我们就可以评估它并告诉你哪里出了问题,这是没有太大帮助的。你正在遵循一条线,在这里你希望有人快速解决你的问题并解决它,而这对你和其他人来说都是一个学习的地方。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32294389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档