MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)

一、文件夹的创建

 1 void CFileOperationDlg::OnButtonMakeFolder() 
 2 {
 3     // TODO: Add your control notification handler code here
 4     UpdateData(TRUE);
 5     CFileFind m_sFileFind;
 6 
 7     if (!m_sFileFind.FindFile(m_FolderName))
 8     {
 9         CreateDirectory(m_FolderName,NULL);
10     }
11 }

二、文件的创建

1 void CFileOperationDlg::OnButtonMakeFile() 
2 {
3     // TODO: Add your control notification handler code here
4     UpdateData(TRUE);
5     CFile m_sFile;
6     m_sFile.Open(m_FolderName + TEXT("\\") + m_FileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
7     m_sFile.SeekToEnd();
8     m_sFile.Close();
9 }

三、文件夹的复制(包括文件的复制)

 1 void CFileOperationDlg::OnButtonCopy() 
 2 {
 3     // TODO: Add your control notification handler code here
 4     UpdateData(TRUE);
 5     CString m_strDestPath;
 6     m_strDestPath = "D:\\TestMyself\\FileOperation\\Debug";
 7     CString m_strSrcPath = "D:\\TestMyself\\FileOperation\\VISTA";
 8     CopyDirectory(m_strSrcPath,m_strDestPath);
 9 }
10 
11 BOOL CFileOperationDlg::CopyDirectory(CString strSrcPath,CString strDestPath)
12 {
13     CFileFind m_sFileFind;
14     if (strSrcPath.IsEmpty())
15     {
16         OutputDebugString("源文件名为空,无法进行拷贝!");
17         return FALSE;
18     }
19     if (!m_sFileFind.FindFile(strDestPath))
20     {
21         CreateDirectory(strDestPath,NULL);//创建目标文件夹
22     }
23     CFileFind finder;
24     CString path;
25     path.Format("%s/*.*",strSrcPath);
26     //AfxMessageBox(path);
27     BOOL bWorking = finder.FindFile(path);
28     while (bWorking)
29     {
30         bWorking = finder.FindNextFile();
31         //AfxMessageBox(finder.GetFileName());
32         if (finder.IsDirectory() && !finder.IsDots())//是文件夹 而且 名称不含 . 或 ..  
33         {
34             CopyDirectory(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName());//递归创建文件夹+"/"+finder.GetFileName()  
35         }
36         else
37         {//是文件,则直接复制
38             //AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()  
39             CopyFile(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName(),FALSE);
40         }
41     }
42 
43     return TRUE;
44 }
45 
46 CString CFileOperationDlg::GetFilePath()
47 {
48     CString m_FilePath;
49 
50     GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
51     m_FilePath.ReleaseBuffer();
52 
53     int m_iPosIndex;
54     m_iPosIndex = m_FilePath.ReverseFind('\\');
55     m_FilePath = m_FilePath.Left(m_iPosIndex+1);
56 
57     return m_FilePath;
58 }
59 
60 CString CFileOperationDlg::GetFileName()
61 {
62     CString sFileName;
63     
64     sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log"); 
65     
66     return sFileName; 
67 }

四、文件夹的删除

 1 BOOL CFileOperationDlg::DeleteFolder(LPCTSTR lpszPath)
 2 {
 3     int nLength = strlen(lpszPath);
 4     char *NewPath = new char[nLength + 2];
 5     strcpy(NewPath,lpszPath);
 6     NewPath[nLength] = '\0';
 7     NewPath[nLength + 1] = '\0';
 8     SHFILEOPSTRUCT FileOp;
 9     ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
10     FileOp.fFlags = FOF_NOCONFIRMATION;
11     FileOp.hNameMappings = NULL;
12     FileOp.hwnd = NULL;
13     FileOp.lpszProgressTitle = NULL;
14     FileOp.pFrom = NewPath;
15     FileOp.pTo = NULL;
16     FileOp.wFunc = FO_DELETE;
17     return SHFileOperation(&FileOp) == 0;
18 }

五、文件夹的移动(剪切)

 1 BOOL CFileOperationDlg::MoveFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
 2 {
 3     int nLengthFrm = strlen(lpszFromPath);
 4     char *NewPathFrm = new char[nLengthFrm + 2];
 5     strcpy(NewPathFrm,lpszFromPath);
 6     NewPathFrm[nLengthFrm] = '\0';
 7     NewPathFrm[nLengthFrm + 1] = '\0';
 8     SHFILEOPSTRUCT FileOp;
 9     ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
10     FileOp.fFlags = FOF_NOCONFIRMATION ;
11     FileOp.hNameMappings = NULL;
12     FileOp.hwnd = NULL;
13     FileOp.lpszProgressTitle = NULL;
14     FileOp.pFrom = NewPathFrm;
15     FileOp.pTo = lpszToPath;
16     FileOp.wFunc = FO_MOVE;
17     
18     return SHFileOperation(&FileOp) == 0;    
19 }

六、文件写操作

 1 void CFileOperationDlg::OnButtonOk() 
 2 {
 3     // TODO: Add your control notification handler code here
 4     UpdateData(TRUE);
 5     try
 6     {
 7         CFile m_sFile;
 8         CFileFind m_FileFind;
 9         CString m_sErrorMessage;
10         //CString m_sFileName = GetFileName(); 
11         //CString m_sFilePath = GetFilePath(); 
12         CString m_sCurrentTime = (CTime::GetCurrentTime()).Format("%Y-%m-%d %X");
13 
14         if (!m_FileFind.FindFile(m_FolderName))
15         {
16             CreateDirectory(m_FolderName,NULL);
17         }
18          m_sFile.Open(m_FolderName + TEXT("\\") +m_FileName,CFile::modeCreate |CFile::modeNoTruncate| CFile::modeReadWrite |CFile::shareDenyWrite); 
19 
20          m_sFile.SeekToEnd();
21 
22          if (sizeof(TCHAR) == sizeof(WCHAR))
23          {
24              WORD wSignature = 0xFFFF;
25              m_sFile.Write(&wSignature,2);
26          }
27 
28          m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************")+TEXT("\r\n") ;
29          m_sFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR));
30 
31          m_RichText += TEXT("\r\n");\
32          m_sFile.Write(m_RichText,m_RichText.GetLength()*sizeof(TCHAR));
33          m_sFile.Close();
34     }
35     catch(CFileException fileException) 
36     { 
37         return ; 
38     }
39     
40 }

 

以上代码测试通过!!

 

  • 4
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值