MFC 复制文件

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;
}
}
  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值