文件复制和移动操作小结

 

在程序设计过程中,经常会遇到对文件的复制和移动等操作,这里主要涉及两个函数:CopyFileMoveFile,详细函数结构如下:(源自MSDN

CopyFile

The CopyFile function copies an existing file to a new file.

The CopyFileEx function provides two additional capabilities. CopyFileEx can call a specified callback function each time a portion of the copy operation is completed, and CopyFileEx can be canceled during the copy operation.

BOOL CopyFile(

  LPCTSTR lpExistingFileName,

  LPCTSTR lpNewFileName,

  BOOL bFailIfExists

);

Parameters

lpExistingFileName

[in] Pointer to a null-terminated string that specifies the name of an existing file.

lpNewFileName

[in] Pointer to a null-terminated string that specifies the name of the new file.

bFailIfExists

[in] If this parameter is TRUE and the new file specified by lpNewFileName already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError

MoveFile

The MoveFile function moves an existing file or a directory, including its children.

To specify how to move the file, use the MoveFileEx function.

BOOL MoveFile(

  LPCTSTR lpExistingFileName,

  LPCTSTR lpNewFileName

);

Parameters

lpExistingFileName

[in] Pointer to a null-terminated string that names an existing file or directory.

lpNewFileName

[in] Pointer to a null-terminated string that specifies the new name of a file or directory. The new name must not already exist. A new file may be on a different file system or drive. A new directory must be on the same drive.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

为了巩固对这两个函数的使用,编写以下案例,函数运行图如下:

本程序是基于对话框的,两个编辑框用来显示源文件目录和目标文件目录,编辑框后的两个按钮是通过调用文件对话框来制定两个目录的详细路径。复制按钮和移动按钮中实现了对文件的复制和移动操作,详细实现见下面代码:

void CFile_Copy_Move_DemoDlg::OnBnClickedButton1()

{

     // 打开制定路径源文件

     CFileDialog SrcDlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"All Files(*.*)|*.*||",AfxGetMainWnd(),0);

     if(SrcDlg.DoModal()==IDOK)

     {

         strSrcPath=SrcDlg.GetPathName();

         fileName=SrcDlg.GetFileName();

         fileExt=SrcDlg.GetFileExt();

     }

     m_EdtSrc.SetWindowTextW((LPCTSTR)strSrcPath);

}

void CFile_Copy_Move_DemoDlg::OnBnClickedButton2()

{

     // 设定目标文件保存路径

     CFileDialog DestDlg(false,fileExt,fileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"All Files(*.*)|*.*||",AfxGetMainWnd(),0);

     if(DestDlg.DoModal()==IDOK)

         strDestPath=DestDlg.GetPathName();

     m_EdtDest.SetWindowTextW((LPCTSTR)strDestPath);

}

void CFile_Copy_Move_DemoDlg::OnBnClickedButton3()

{

     // 文件复制操作

     BOOL bReturn=CopyFileEx((LPCWSTR)strSrcPath,(LPCWSTR)strDestPath,NULL,NULL,(LPBOOL)bCancel,0);

     if(true==bReturn)

     {

         MessageBox(L"Successed to copy file!");

     }

     else

         MessageBox(L"Failed to copy file!");

}

void CFile_Copy_Move_DemoDlg::OnBnClickedButton4()

{

     // 文件移动操作

     BOOL bReturn=MoveFile((LPCWSTR)strSrcPath,(LPCWSTR)strDestPath);

     if(true==bReturn)

     {

         MessageBox(L"Successed fo move files!");

     }

     else

         MessageBox(L"Failed to move file!");

}

最后,总结一下程序的流程:先通过单击源目录后面的按钮,触发文件打开对话框,通过制定文件,确定源文件路径,并在编辑框中显示出来。在通过单击目标目录后面的按钮,触发文件保存对话框,通过指定保存路径,确定目标文件路径,并在编辑框中显示出来。然后可以选择对文件进行复制操作或移动操作,并通过MessageBox消息对话框来提示是否操作成功。程序中还涉及考文件对话框CFileDialog的相关知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值