码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-19 20:10:37      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   os   sp   for   

一、文件夹的创建

 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 }

 

以上代码测试通过!!

 

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

标签:des   style   blog   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/wanzaiyimeng/p/4108801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!