文件的複製、移動與刪除

//nIndex:標誌操作  0:複製  1:刪除  2:移動
//strSourceDir: 被操作的文件
//strDenstinationDir:複製或移動的目標路徑,刪除時該值為空
void OnExecute(int nIndex, CString& strSourceDir, CString& strDestinationDir)
{
DWORD errorCode;
CString strMsg,strHint;
CString strPre,strNxt;
//由於fileOpStruct.pForm的特殊要求,其必須以null結尾,否則不會成功,故這裡需要在尾部加上null字符
strSourceDir.Insert(strSourceDir.GetLength()+1,'\0');
strDestinationDir.Insert(strDestinationDir.GetLength()+1,'\0');


fileOpStruct.pFrom = strSourceDir;
fileOpStruct.pTo = strDestinationDir;


int k = strDestinationDir.GetLength();
switch(nIndex){
case 0: //copy
//準備提示信息
strPre = "將文件";
strNxt = "中!";
strPre = strPre+strSourceDir+"複製到"+strDestinationDir+strNxt;
strHint="複製被中斷";
strMsg = "複製出錯,其代碼為:";
//填充結構的成員
fileOpStruct.lpszProgressTitle = strPre;
fileOpStruct.wFunc = FO_COPY;
break;
case 1: //delete
//準備提示信息
strPre = "將文件";
strNxt = "刪除!";
strPre = strPre+strSourceDir+strNxt;
strHint="刪除被中斷";
strMsg = "刪除出錯,其代碼為:";
//填充結構的成員
fileOpStruct.lpszProgressTitle = strPre;
fileOpStruct.wFunc = FO_DELETE;
break;
case 2: //move
//準備提示信息
strPre = "將文件";
strNxt = "中!";
strPre = strPre+strSourceDir+"移動到"+strDestinationDir+strNxt;
strHint="移動被中斷";
strMsg = "移動出錯,其代碼為:";
//填充結構的成員
fileOpStruct.lpszProgressTitle = strPre;
fileOpStruct.wFunc = FO_MOVE;
break;
default:
break;
}


//填充結構成員
fileOpStruct.fFlags = FOF_SIMPLEPROGRESS;
int res = SHFileOperation(&fileOpStruct);
errorCode = GetLastError();
if(fileOpStruct.fAnyOperationsAborted == TRUE)
{
//提示中斷信息
AfxMessageBox(strHint);
}
else
{
//如果操作出錯
if(res!=0)
{
CString strTem;
strTem.Format("%s%d",strMsg,errorCode);
AfxMessageBox(strTem);
}
}
return;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章