MFC - 删除指定文件夹

百般思念 提交于 2020-04-02 21:00:15
 1 // 删除指定的文件夹
 2 void DeleteDirectory(CString strDir)
 3 {
 4     if (strDir.IsEmpty())
 5     {
 6         RemoveDirectory(strDir);
 7         return;
 8     }
 9 
10     //首先删除文件及子文件夹 
11     CFileFind   ff;
12     BOOL bFound = ff.FindFile(strDir + _T("\\*"), 0);
13     while (bFound)
14     {
15         bFound = ff.FindNextFile();
16         if (ff.GetFileName() == _T(".") || ff.GetFileName() == _T(".."))        continue;
17 
18         //去掉文件(夹)只读等属性 
19         SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
20         if (ff.IsDirectory())
21         {
22             //递归删除子文件夹 
23             DeleteDirectory(ff.GetFilePath());
24             RemoveDirectory(ff.GetFilePath());
25         }
26         else
27         {
28             DeleteFile(ff.GetFilePath());   //删除文件 
29         }
30 
31     }
32 
33     ff.Close();
34 
35     //然后删除该文件夹 
36     RemoveDirectory(strDir);
37 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!