有如下代码,希望将sourceDirName目录下的文件移动到destDirName,执行正常:我的问题是,为什么如果把FileOP.pFrom 改为 test,则无法达到目的。
用debug跟踪变量的值可以看到sourceDirName 和 test是完全相同的两个字符串,
另外strcmp的返回值0也可以说明这个问题。
sourceDirName和test相比只是在Format时在*.*前加了一个空字符串,其实也就是什么也没有加。
CString destDirName, sourceDirName, test;destDirName = this->m_EfsDlg.root_dir_name;  //用destDirName做temp
   destDirName = destDirName.Right(destDirName.GetLength() - destDirName.ReverseFind('/') - 1);
   sourceDirName.Format("%s\\temp\\%s\\%s*.*\0", this->m_sPerDir, destDirName, test);
   test.Format("%s\\temp\\%s\\*.*\0", this->m_sPerDir, destDirName);
   int a = strcmp(sourceDirName, test);
   //sourceDirName.SetAt(sourceDirName.GetLength()-1,'\0');
   destDirName.Format("%s\0",this->m_EfsDlg.m_DownloadDestDir);   FileOp.hwnd = NULL;
   FileOp.wFunc = FO_MOVE;
   FileOp.pFrom = sourceDirName;
   FileOp.pTo = destDirName;
   FileOp.fFlags = FOF_NOERRORUI; //| FOF_RENAMEONCOLLISION;
   FileOp.fAnyOperationsAborted = false;
   FileOp.hNameMappings = NULL;
   FileOp.lpszProgressTitle = NULL;
   
   SHFileOperation(&FileOp);

解决方案 »

  1.   

    天狼得回帖提示了我,在format中格式化字符串结尾直接加'\0'是不行的,因为format遇到第一个'\0'就认为是字符串的结尾。
    应该是:
    sourceDirName.Format("%s\\temp\\%s\\*.*a",  this->m_sPerDir,  destDirName,)
    sourceDirName.SetAt(sourceDirName.GetLength()-1,'\0')  //结尾的a是用来占位的。至于在中间插入空字符串,为什么会在结尾多一个'\0',我也找不出原因。天狼,谢谢了。