MFC版 文件拷贝
所用函数:
BOOL CopyFile( LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists );
LPCTSTR lpExistingFileName 已存在文件名,如:D://123.prt
LPCTSTR lpNewFileName 复制的新文件名,如:D://Program file//123.prt
BOOL bFailIfExists 如果新文件名已存在是否覆盖。true:不覆盖 false:覆盖
void FileCopyTo(CString source, CString destination, CString searchStr, BOOL cover )
{
CString strSourcePath = source;
CString strDesPath = destination;
CString strFileName = searchStr;
CFileFind filefinder;
CString strSearchPath = strSourcePath + "//" + strFileName;
CString filename;
BOOL bfind = filefinder.FindFile(strSearchPath);
CString SourcePath, DisPath;
while (bfind)
{
bfind = filefinder.FindNextFile();
filename = filefinder.GetFileName();
SourcePath = strSourcePath + "//" + filename;
DisPath = strDesPath + "//" + filename;
CopyFile((LPCSTR)SourcePath, (LPCSTR)DisPath, true);
}
filefinder.Close();
}
void main()
{
char *sor="E://fox_work//model//";
char *des="E://fox_work//";
char *filename="liangan.prt";
FileCopyTo(sor,des,filename,true);
}
这样就将E:/fox_work/model/目录下的liangan.prt文件复制到E:/fox_work/目录下了。
还可以
BOOL CopyFileEx(LPCSTR lpszSrc, LPCSTR lpszDec,DWORD dwFlag)
{
SHFILEOPSTRUCT lpFileOp;
lpFileOp.hwnd=AfxGetMainWnd()->GetSafeHwnd();
lpFileOp.wFunc=FO_COPY;
lpFileOp.pFrom=lpszSrc;
lpFileOp.pTo=lpszDec;
lpFileOp.fFlags=dwFlag;
lpFileOp.fAnyOperationsAborted=FALSE;
lpFileOp.hNameMappings =NULL;
lpFileOp.lpszProgressTitle =NULL;
int rval=SHFileOperation(&lpFileOp);
CString strMsg;
if(rval==0)
{
if(lpFileOp.fAnyOperationsAborted==TRUE)
{
strMsg="复制文件";
strMsg+=lpszSrc;
strMsg += "操作被取消!";
::AfxMessageBox(strMsg,MB_OK);
return FALSE;
}
else
{
::AfxMessageBox("复制文件操作成功!",MB_OK);
return TRUE;
}
}
else
{
strMsg="复制文件从";
strMsg+=lpszSrc;
strMsg+=" 到 ";
strMsg+=lpszDec;
strMsg+=" 失败!";
::AfxMessageBox(strMsg,MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
}