MFC文件操作大全

获得文件路径:GetFilePath()
获得文件名:GetFileName()
获得文件标题:GetFileTitile()
Path is : "C:\WINDOWS\SYSTEM.INI"
Name is : "SYSTEM.INI"
Title is: "System"

1.创建文件夹

创建路径上最后的一个文件夹
[cpp]  view plain  copy
  1. CreateDirectory(%%1,NULL)  
创建路径上所有的文件夹
[cpp]  view plain  copy
  1. SHCreateDirectoryEx(NULL, strTempPath, NULL);  

2.创建文件

[cpp]  view plain  copy
  1. CFile file;   
  2. file.Open(%%1,CFile::modeCreate|CFile::modeWrite);  

3.删除文件

[cpp]  view plain  copy
  1. DeleteFile(%%1);  

4.删除文件夹

[cpp]  view plain  copy
  1. RemoveDirectory(%%1);  

5.删除一个文件夹下所有的文件夹

[cpp]  view plain  copy
  1. CFileFind finder;   
  2. BOOL bWorking = finder.FindFile(%%1+"\\*.*");   
  3. while (bWorking)   
  4. {   
  5. bWorking = finder.FindNextFile();  
  6. if(finder.IsDirectory()){  
  7. dir.Delete(finder.GetFilePath());  
  8. }  
  9. }   

6.清空文件夹

[cpp]  view plain  copy
  1. RemoveDirectory(%%1);  
  2. CreateDirectory(%%1,NULL)  

7.读取文件

[cpp]  view plain  copy
  1. char sRead[1024];  
  2. CFile mFile(_T(%%1),CFile::modeRead);  
  3. while(sRead!=null)  
  4. {  
  5.   mFile.Read(sRead,1024);  
  6. CString %%2=CString(sRead);  
  7. %%3  
  8. }  

8.写入文件

[cpp]  view plain  copy
  1. mFile.Close();  
  2. CFile mFile(_T(%%1),  CFile::modeWrite|CFile::modeCreate);  
  3. mFile.Write(%%2);  
  4. mFile.Flush();  
  5. mFile.Close();  

9.写入随机文件

[cpp]  view plain  copy
  1. char szTempPath[_MAX_PATH],szTempfile[_MAX_PATH];  
  2. GetTempPath(_MAX_PATH, szTempPath);  
  3. GetTempFileName(szTempPath,_T ("my_"),0,szTempfile);  
  4. CFile m_tempFile(szTempfile,CFile:: modeCreate|CFile:: modeWrite);  
  5. char m_char='a';  
  6. m_tempFile.Write(&m_char,2);  
  7. m_tempFile.Close();  
  8. //循环写入多个值  
  9. strTempA;  
  10. int i;  
  11. int nCount=6;  
  12. file://共有6个文件名需要保存  
  13. for(i=0;i {strTemp.Format("%d",i);  
  14. strTempA=文件名;  
  15. file://文件名可以从数组,列表框等处取得.  
  16. ::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,  
  17. c:\\usefile\\usefile.ini);  
  18. }  
  19. strTemp.Format("%d",nCount);  
  20. ::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");  
  21. file://将文件总数写入,以便读出.  
  22. //读出  
  23. nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");  
  24. for(i=0;i {strTemp.Format("%d",i);  
  25. strTemp="FileName"+strTemp;  
  26. ::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");  
  27. file://使用strTempA中的内容.  
  28. }  

10.读取文件属性

[cpp]  view plain  copy
  1. dwAttrs   =   GetFileAttributes(%%1);   
  2. if   (dwAttrs   &   FILE_ATTRIBUTE_READONLY)       
  3. {       
  4.   
  5.   
  6. }       
  7. if   (NORMAL &  FILE_ATTRIBUTE_READONLY)  
  8. {       
  9.   
  10.   
  11. }       

11.写入属性

[cpp]  view plain  copy
  1. SetFileAttributes(szNewPath,dwAttrs   |   FILE_ATTRIBUTE_READONLY);      

12.枚举一个文件夹中的所有文件夹

[cpp]  view plain  copy
  1. CFileFind finder;   
  2. BOOL bWorking = finder.FindFile(%%1+"\\*.*");   
  3. while (bWorking)   
  4. {   
  5. bWorking = finder.FindNextFile();  
  6. if(finder.IsDirectory()){  
  7. CString %%1=finder.GetFilePath();  
  8. %%2  
  9. }  
  10. }   

13.复制文件夹

[cpp]  view plain  copy
  1. WIN32_FIND_DATA FileData;   
  2. HANDLE hSearch;   
  3. DWORD dwAttrs;   
  4. char szDirPath[] = %%2;   
  5. char szNewPath[MAX_PATH];   
  6. char szHome[MAX_PATH];   
  7. BOOL fFinished = FALSE;   
  8. if (!CreateDirectory(szDirPath, NULL))   
  9. {  
  10. //不能创建新的目录   
  11. return;  
  12. }  
  13. hSearch = FindFirstFile(%%1+"\\*.*", &FileData);   
  14. if (hSearch == INVALID_HANDLE_VALUE)   
  15. {   
  16. return;   
  17. }   
  18. while (!fFinished)   
  19. {   
  20. lstrcpy(szNewPath, szDirPath);   
  21. lstrcat(szNewPath, FileData.cFileName);   
  22. if (CopyFile(FileData.cFileName, szNewPath, FALSE))   
  23. {   
  24. dwAttrs = GetFileAttributes(FileData.cFileName);   
  25. if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))   
  26. {   
  27. SetFileAttributes(szNewPath,   
  28. dwAttrs | FILE_ATTRIBUTE_READONLY);   
  29. }   
  30. }   
  31. else   
  32. {   
  33. //不能复制文件  
  34. return;   
  35. }   
  36. if (!FindNextFile(hSearch, &FileData))   
  37. {   
  38. if (GetLastError() == ERROR_NO_MORE_FILES)   
  39. {   
  40. //遍历文件夹完成   
  41. fFinished = TRUE;   
  42. }   
  43. else   
  44. {   
  45. //找不到下一个文件  
  46. return;   
  47. }   
  48. }   
  49. }   
  50. FindClose(hSearch);   

14.复制一个文件夹下所有的文件夹到另一个文件夹下

[cpp]  view plain  copy
  1. WIN32_FIND_DATA FileData;   
  2. HANDLE hSearch;   
  3. DWORD dwAttrs;   
  4. char szDirPath[] = %%2;   
  5. char szNewPath[MAX_PATH];   
  6. char szHome[MAX_PATH];   
  7. BOOL fFinished = FALSE;   
  8. if (!CreateDirectory(szDirPath,NULL))   
  9. {  
  10. //不能创建新的目录   
  11. return;  
  12. }  
  13. BOOL bWorking = finder.FindFile(%%1+"\\*.*");   
  14. while (bWorking)   
  15. {   
  16. bWorking = finder.FindNextFile();  
  17. if(finder.IsDirectory()){  
  18. hSearch = FindFirstFile(finder.GetFilePath()+"\\*.*", &FileData);   
  19. if (hSearch == INVALID_HANDLE_VALUE)   
  20. {   
  21. return;   
  22. }   
  23. while (!fFinished)   
  24. {   
  25. lstrcpy(szNewPath, szDirPath);   
  26. lstrcat(szNewPath, FileData.cFileName);   
  27. if (CopyFile(FileData.cFileName, szNewPath, FALSE))   
  28. {   
  29. dwAttrs = GetFileAttributes(FileData.cFileName);   
  30. if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))   
  31. {   
  32. SetFileAttributes(szNewPath,   
  33. dwAttrs | FILE_ATTRIBUTE_READONLY);   
  34. }   
  35. }   
  36. else   
  37. {   
  38. //不能复制文件  
  39. return;   
  40. }   
  41. if (!FindNextFile(hSearch, &FileData))   
  42. {   
  43. if (GetLastError() == ERROR_NO_MORE_FILES)   
  44. {   
  45. //遍历文件夹完成   
  46. fFinished = TRUE;   
  47. }   
  48. else   
  49. {   
  50. //找不到下一个文件  
  51. return;   
  52. }   
  53. }   
  54. }   
  55. FindClose(hSearch);  
  56. }  
  57. }   

15.移动文件夹

[cpp]  view plain  copy
  1. WIN32_FIND_DATA FileData;   
  2. HANDLE hSearch;   
  3. DWORD dwAttrs;   
  4. char szDirPath[] = %%2;   
  5. char szNewPath[MAX_PATH];   
  6. char szHome[MAX_PATH];   
  7. BOOL fFinished = FALSE;   
  8. if (!CreateDirectory(szDirPath, NULL))   
  9. {  
  10. //不能创建新的目录   
  11. return;  
  12. }  
  13. hSearch = FindFirstFile(%%1+"\\*.*", &FileData);   
  14. if (hSearch == INVALID_HANDLE_VALUE)   
  15. {   
  16. return;   
  17. }   
  18. while (!fFinished)   
  19. {   
  20. lstrcpy(szNewPath, szDirPath);   
  21. lstrcat(szNewPath, FileData.cFileName);   
  22. if (CopyFile(FileData.cFileName, szNewPath, FALSE))   
  23. {   
  24. dwAttrs = GetFileAttributes(FileData.cFileName);   
  25. if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))   
  26. {   
  27. SetFileAttributes(szNewPath,   
  28. dwAttrs | FILE_ATTRIBUTE_READONLY);   
  29. }   
  30. }   
  31. else   
  32. {   
  33. //不能复制文件  
  34. return;   
  35. }   
  36. if (!FindNextFile(hSearch, &FileData))   
  37. {   
  38. if (GetLastError() == ERROR_NO_MORE_FILES)   
  39. {   
  40. //遍历文件夹完成   
  41. fFinished = TRUE;   
  42. }   
  43. else   
  44. {   
  45. //找不到下一个文件  
  46. return;   
  47. }   
  48. }   
  49. }   
  50. FindClose(hSearch);   
  51. RemoveDirectory(%%1);  

16.移动一个文件夹下所有的文件夹到另一个目录下

[cpp]  view plain  copy
  1. WIN32_FIND_DATA FileData;   
  2. HANDLE hSearch;   
  3. DWORD dwAttrs;   
  4. char szDirPath[] = %%2;   
  5. char szNewPath[MAX_PATH];   
  6. char szHome[MAX_PATH];   
  7. BOOL fFinished = FALSE;   
  8. if (!CreateDirectory(szDirPath,NULL))   
  9. {  
  10. //不能创建新的目录   
  11. return;  
  12. }  
  13. BOOL bWorking = finder.FindFile(%%1+"\\*.*");   
  14. while (bWorking)   
  15. {   
  16. bWorking = finder.FindNextFile();  
  17. if(finder.IsDirectory()){  
  18. hSearch = FindFirstFile(finder.GetFilePath()+"\\*.*", &FileData);   
  19. if (hSearch == INVALID_HANDLE_VALUE)   
  20. {   
  21. return;   
  22. }   
  23. while (!fFinished)   
  24. {   
  25. lstrcpy(szNewPath, szDirPath);   
  26. lstrcat(szNewPath, FileData.cFileName);   
  27. if (CopyFile(FileData.cFileName, szNewPath, FALSE))   
  28. {   
  29. dwAttrs = GetFileAttributes(FileData.cFileName);   
  30. if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))   
  31. {   
  32. SetFileAttributes(szNewPath,   
  33. dwAttrs | FILE_ATTRIBUTE_READONLY);   
  34. }   
  35. }   
  36. else   
  37. {   
  38. //不能复制文件  
  39. return;   
  40. }   
  41. if (!FindNextFile(hSearch, &FileData))   
  42. {   
  43. if (GetLastError() == ERROR_NO_MORE_FILES)   
  44. {   
  45. //遍历文件夹完成   
  46. fFinished = TRUE;   
  47. }   
  48. else   
  49. {   
  50. //找不到下一个文件  
  51. return;   
  52. }   
  53. }   
  54. }   
  55. FindClose(hSearch);  
  56. RemoveDirectory(finder.GetFilePath().GetBuffer(0));  
  57. }  
  58. }  

17.以一个文件夹的框架在另一个目录创建文件夹和空文件

[cpp]  view plain  copy
  1. WIN32_FIND_DATA FileData;   
  2. HANDLE hSearch;   
  3. DWORD dwAttrs;   
  4. char szDirPath[] = %%2;   
  5. char szNewPath[MAX_PATH];   
  6. char szHome[MAX_PATH];   
  7. BOOL fFinished = FALSE;   
  8. if (!CreateDirectory(szDirPath, NULL))   
  9. {  
  10. //不能创建新的目录   
  11. return;  
  12. }  
  13. hSearch = FindFirstFile(%%1+"\\*.*", &FileData);   
  14. if (hSearch == INVALID_HANDLE_VALUE)   
  15. {   
  16. return;   
  17. }   
  18. while (!fFinished)   
  19. {   
  20. lstrcpy(szNewPath, szDirPath);   
  21. lstrcat(szNewPath, FileData.cFileName);   
  22. HANDLE hFile=CreateFileHandle hFile=CreateFile(szNewPath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);  
  23. if(!hFile)  
  24. {   
  25. //不能创建文件  
  26. return;   
  27. }   
  28. if (!FindNextFile(hSearch, &FileData))   
  29. {   
  30. if (GetLastError() == ERROR_NO_MORE_FILES)   
  31. {   
  32. //遍历文件夹完成   
  33. fFinished = TRUE;   
  34. }   
  35. else   
  36. {   
  37. //找不到下一个文件  
  38. return;   
  39. }   
  40. }   
  41. }   
  42. FindClose(hSearch);   

18.复制文件

[cpp]  view plain  copy
  1. CopyFile(%%1,%%2,true)  

19.复制一个文件夹下所有的文件到另一个目录

[cpp]  view plain  copy
  1. //#include <string>  
  2. using std::string;  
  3. char sep='/';  
  4. #ifdef _WIN32  
  5. sep='\\';  
  6. #endif  
  7. CFileFind finder;   
  8. BOOL bWorking = finder.FindFile(%%1+"\\*.*");   
  9. while (bWorking)   
  10. {   
  11. bWorking = finder.FindNextFile();  
  12. if(!finder.IsDirectory() || finder.IsDots()){  
  13. string s(finder.GetFileName());  
  14. CString sourcefile(%%1);  
  15. if(s.rfind(sep,s.length())!=string::npos)  
  16. {  
  17. sourcefile=sourcefile+"\\"+s.substr(i+1,s.length()-i);  
  18. CString targetfile(s.substr(i+1,s.length()-i));  
  19. targetfile=%%2+"\\"+targetfile;  
  20. CopyFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);  
  21. }  
  22. }  
  23. }  

20.提取扩展名

[cpp]  view plain  copy
  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. size_t i=s.rfind('.',s.length());  
  5. if(i!=string::npos)  
  6. CString %%2(s.substr(i+1,s.length()-i));  
  7. else  
  8. CString %%2="";  

21.提取文件名

[cpp]  view plain  copy
  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. char sep='/';  
  5. #ifdef _WIN32  
  6. sep='\\';  
  7. #endif  
  8. size_t i=s.rfind(sep,s.length());  
  9. if(i!=string::npos)  
  10. CString %%2(s.substr(i+1,s.length()-i));  
  11. else  
  12. CString %%2="";  

22.提取文件路径

[cpp]  view plain  copy
  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. char sep='/';  
  5. #ifdef _WIN32  
  6. sep='\\';  
  7. #endif  
  8. size_t i=s.rfind(sep,s.length());  
  9. if(i!=string::npos)  
  10. CString %%2(s.substr(0,i));  
  11. else  
  12. CString %%2="";  

23.替换扩展名

[cpp]  view plain  copy
  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. string newExt(%%2);  
  5. string::size_type i=s.rfind('.',s.length());  
  6. if(i!=string::npos)  
  7. s.replace(i+1,newExt.length(),newExt);  
  8. CString %%3(s);  

24.追加路径

[cpp]  view plain  copy
  1. //#include <string>  
  2. //#include <cstdlib>  
  3. //#include <boost/filesystem/operations.hpp>  
  4. //#include <boost/filesystem/fstream.hpp>  
  5. using namespace std;  
  6. using namespace boost::filesystem;  
  7. try {  
  8. path p1=complete(path(%%2,native),  
  9. path(%%1,native));  
  10. path p2=system_complete(path(%%2,native));  
  11. CString %%3(p3);  
  12. }  
  13. catch(exception& e){  
  14. //e.what();  
  15. }  

25.移动文件

[cpp]  view plain  copy
  1. MoveFile(%%1,%%2);  

26.移动一个文件夹下所有文件到另一个目录

[cpp]  view plain  copy
  1. //#include <string>  
  2. using std::string;  
  3. char sep='/';  
  4. #ifdef _WIN32  
  5. sep='\\';  
  6. #endif  
  7. CFileFind finder;   
  8. BOOL bWorking = finder.FindFile(%%1+"\\*.*");   
  9. while (bWorking)   
  10. {   
  11. bWorking = finder.FindNextFile();  
  12. if(!finder.IsDirectory() || finder.IsDots()){  
  13. string s(finder.GetFileName());  
  14. CString sourcefile(%%1);  
  15. if(s.rfind(sep,s.length())!=string::npos)  
  16. {  
  17. sourcefile=sourcefile+"\\"+s.substr(i+1,s.length()-i);  
  18. CString targetfile(s.substr(i+1,s.length()-i));  
  19. targetfile=%%2+"\\"+targetfile;  
  20. MoveFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);  
  21. }  
  22. }  
  23. }  

27.指定目录下搜索文件

[cpp]  view plain  copy
  1. CString strFileTitle;  
  2. CFileFind finder;  
  3. BOOL bWorking = finder.FindFile  ("C:\\windows\\sysbkup\\*.cab");  
  4. while(bWorking)  
  5. {  
  6. bWorking=finder.FindNextFile();  
  7. strFileTitle=finder.GetFileTitle();  
  8. }  
 

猜你喜欢

转载自blog.csdn.net/kulala082/article/details/78094336
今日推荐