文件的复制、移动与删除

//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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章