MFC - 删除指定文件夹

 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 }

 

posted @ 2016-04-16 20:51  C/C++/Python/Java  阅读(1989)  评论(0编辑  收藏  举报