//删除文件或者文件夹
bool DeleteFile(string strPath)
{
int nLen = strPath.length();
char* pPath = new char[nLen+2];
strcpy(pPath,strPath.c_str());
pPath[nLen+1]='\0';
SHFILEOPSTRUCT FileOp={0};
FileOp.fFlags = //FOF_ALLOWUNDO | //允许放回回收站
FOF_NOCONFIRMATION | //不出现确认对话框
FOF_NOERRORUI; //不出现错误对话框
FileOp.pFrom = pPath;
FileOp.pTo = NULL; //一定要是NULL
FileOp.wFunc = FO_DELETE; //删除操作
bool bResult = (SHFileOperation(&FileOp) == 0);
delete []pPath;
return bResult;
}
//复制文件或文件夹
bool CopyFile(string strFrom,string strTo)
{
int nFromLen = strFrom.length();
int nToLen = strTo.length();
char* pFrom = new char[nFromLen+2];
char* pTo = new char[nToLen+2];
strcpy(pFrom,strFrom.c_str());
strcpy(pTo,strTo.c_str());
pFrom[nFromLen+1]='\0';
pTo[nToLen+1]='\0';
SHFILEOPSTRUCT FileOp={0};
FileOp.fFlags = FOF_NOCONFIRMATION| //不出现确认对话框
FOF_NOCONFIRMMKDIR ; //需要时直接创建一个文件夹,不需用户确定
FileOp.pFrom = pFrom;
FileOp.pTo = pTo;
FileOp.wFunc = FO_COPY;
bool bResult = (SHFileOperation(&FileOp) == 0);
delete []pFrom;
delete []pTo;
return bResult;
}
//移动文件或文件夹
bool MoveFile(string strFrom,string strTo)
{
int nFromLen = strFrom.length();
int nToLen = strTo.length();
char* pFrom = new char[nFromLen+2];
char* pTo = new char[nToLen+2];
strcpy(pFrom,strFrom.c_str());
strcpy(pTo,strTo.c_str());
pFrom[nFromLen+1]='\0';
pTo[nToLen+1]='\0';
SHFILEOPSTRUCT FileOp={0};
FileOp.fFlags = FOF_NOCONFIRMATION| //不出现确认对话框
FOF_NOCONFIRMMKDIR ; //需要时直接创建一个文件夹,不需用户确定
FileOp.pFrom = pFrom;
FileOp.pTo = pTo;
FileOp.wFunc = FO_MOVE;
bool bResult = (SHFileOperation(&FileOp) == 0);
delete []pFrom;
delete []pTo;
return bResult;
}
//从命名文件或文件夹
bool ReNameFile(string strFrom,string strTo)
{
int nFromLen = strFrom.length();
int nToLen = strTo.length();
char* pFrom = new char[nFromLen+2];
char* pTo = new char[nToLen+2];
strcpy(pFrom,strFrom.c_str());
strcpy(pTo,strTo.c_str());
pFrom[nFromLen+1]='\0';
pTo[nToLen+1]='\0';
SHFILEOPSTRUCT FileOp={0};
FileOp.fFlags = FOF_NOCONFIRMATION; //不出现确认对话框
FileOp.pFrom = pFrom;
FileOp.pTo = pTo;
FileOp.wFunc = FO_RENAME;
bool bResult = (SHFileOperation(&FileOp) == 0);
delete []pFrom;
delete []pTo;
return bResult;
}
SHFileOperation操作,如果是相对路径需要后面两个'\0',如"..\\x.xml\0",包括自身的\0
扩展阅读: http://www.vcsos.com/article/pageSource/120216/20120216083823.shtml