请问vc中如何拷贝文件?

banmiton 2009-08-20 08:45:05
请问vc中如果要拷贝某文件到另一路径下,如何实现?谢谢。。。能否跨盘操作(C盘拷贝到d盘),如果是进行移动操作呢?好像是说移动操作不能跨盘符进行吗?
...全文
233 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
WaistCoatX 2009-08-20
  • 打赏
  • 举报
回复
CopyFile

不过目标目录必须存在,否则会失败

创建目录API函数CreateDirectory
MoXiaoRab 2009-08-20
  • 打赏
  • 举报
回复
可以使用CopyFile,也可以使用SHELL函数, SHFileOperation
使用方法见 参考

既可以拷贝单个文件,也可以拷贝文件夹

注意某些目录拷贝的时候需要Debug权限提升才行
zngsai 2009-08-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zoulie 的回复:]
The CopyFile function copies an existing file to a new file.

BOOL CopyFile(
  LPCTSTR lpExistingFileName,
                          // pointer to name of an existing file
  LPCTSTR lpNewFileName,  // pointer to filename to copy to
  BOOL bFailIfExists      // flag for operation if file exists
);


[/Quote]


用这个API就行了!
churenxh 2009-08-20
  • 打赏
  • 举报
回复
char szExtFile[_MAX_FNAME]="C:\\school.txt";
char szNewFile[_MAX_FNAME]="D:\\11.txt";
if (!CopyFile(szExtFile,szNewFile,FALSE))
MessageBox(NULL,"fail","fail",MB_OK);
else
MessageBox(NULL,"success","success",MB_OK);
return 0;


或者这样
int CTestCSDNDlg::CopyFileEx( CString strSrcPath, CString strExt, CString strDesPath )
{
CFileFind ff;
CString szDir = strSrcPath;
CString strSize = _T("");

if(szDir.Right(1) != "\\")
szDir += "\\";

szDir += "*.*";

BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())// 文件夹
{
CString strFolderPath = ff.GetFilePath();
CopyFileEx(strFolderPath, strExt, strDesPath);
}
else if(!ff.IsDirectory() && !ff.IsDots())// 文件
{
// 判断扩展名
CString strFileName = ff.GetFileName();
int nPos;
nPos = strFileName.ReverseFind( _T( '.' ) );
CString strFileExt;
strFileExt = strFileName.Right( strFileName.GetLength() - nPos - 1 );
if ( strFileExt == strExt )
{
// 复制文件
CString strFilePath;
strFilePath = ff.GetFilePath();
CString strNewFilePath;
strNewFilePath = strDesPath + _T( "\\" ) + strFileName;
::CopyFile( strFilePath, strNewFilePath, FALSE );
}
}
}
ff.Close();

return 0;
}
xylicon 2009-08-20
  • 打赏
  • 举报
回复
SHFILEOPSTRUCT shfos;     
shfos.wFunc = FO_COPY;
shfos.pFrom = pPathsFrom; // 原来的路径
shfos.pTo = pPathTo; // 目标路径
shfos.fFlags = FOF_ALLOWUNDO;
shfos.hwnd = hWnd;
SHFileOperation(&shfos);


如果想做移动操作,只需把上面的FO_COPY改成FO_MOVE就行了,可以跨盘符进行。
oyljerry 2009-08-20
  • 打赏
  • 举报
回复
MoveFile不能跨分区
oyljerry 2009-08-20
  • 打赏
  • 举报
回复
CopyFile()
zoulie 2009-08-20
  • 打赏
  • 举报
回复
The CopyFile function copies an existing file to a new file.

BOOL CopyFile(
LPCTSTR lpExistingFileName,
// pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧