本文简单介绍下VC复制和移动文件操作。
首先,新建对话框应用程序,然后添加一个按钮。
为了方便操作,我首先在程序文件夹目录中新建好了一个原始文件夹和目标文件夹
添加功能操作
void CCopyAndMoveFileDlg::OnButton1()
{
//获取当前路径
CString sCurrentPath = "";
char Path[270];
DWORD len = 0;
DWORD i=0;
len = GetCurrentDirectory(270,Path);
for (i=0; i<len; i++)
{
sCurrentPath = sCurrentPath + CString(Path[i]);
}
//将指定源文件夹的文件导入到指定目标文件中
CString sSourceFolderPath = sCurrentPath + "\\MySourceFolder";
CString sTargetFolderPath = sCurrentPath + "\\MyTargetFolder";
CString sFileName = "MyFile.txt";
CString sWarning = "";
CString sSourceFilePathName = sSourceFolderPath + "\\" + sFileName;
CString sTargetFilePathName = sTargetFolderPath + "\\" + sFileName;
if(!_access(sTargetFilePathName,0))
{
sWarning = "是否要覆盖" + sTargetFilePathName + "?";
if(IDYES == MessageBox(sWarning,NULL,MB_YESNO | MB_ICONWARNING))
{
DeleteFile(sTargetFilePathName);
}
else
{
return;
}
}
//复制文件
CString sFromFileList = sSourceFilePathName + '\0';
SHFILEOPSTRUCT lpsh;
ZeroMemory(&lpsh,sizeof(lpsh));
lpsh.hwnd = m_hWnd;
lpsh.fFlags = FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS;
lpsh.wFunc = FO_COPY;
lpsh.pFrom = sFromFileList;
lpsh.pTo = sTargetFolderPath;
if(0 != SHFileOperation(&lpsh))
{
MessageBox("复制文件出错!");
}
else
{
MessageBox("复制文件成功!");
}
}运行效果
点击复制并且移动文件按钮
再次点击复制并且移动文件按钮,发现目标路径下已经存在该文件,于是提示是否需要覆盖
点击是
这篇博客详细讲解了如何在VC环境下实现文件的复制和移动。通过创建对话框应用程序,并添加按钮,作者演示了如何处理文件操作,包括在程序文件夹中创建源文件夹和目标文件夹,以及当目标位置已有同名文件时如何处理覆盖问题。
543

被折叠的 条评论
为什么被折叠?



