删除文件夹以及文件夹下的所有文件
jackyhwei 发布于 2011-11-07 09:14 点击:186次 | |
API函数BOOL WINAPI RemoveDirectory(__in LPCTSTR lpPathName )用来删除文件夹。但是,如果文件夹中有文件或者子文件夹,该函数执行会失败。因此,必须使用递归方式删除子文件夹和文件。
直接上代码: - BOOL DeleteDirectory(wchar_t* wscDirectory)
- {
- CString strDirectory = wscDirectory;
- CString strFile = strDirectory + L"//*.*";
- CFileFind tempFind;
-
- BOOL IsFinded = tempFind.FindFile(strFile);
- while(IsFinded)
- {
- IsFinded = tempFind.FindNextFile();
- if(!tempFind.IsDots())
- {
- CString strFoundFile = tempFind.GetFilePath();
- if(tempFind.IsDirectory())
- {
- DeleteDirectory(strFoundFile.GetBuffer());
- }
- else
- {
- DeleteFile(strFoundFile);
- }
- }
- }
- tempFind.Close();
- if(!RemoveDirectory(strDirectory))
- {
- return FALSE;
- }
- return TRUE;
- }
API函数BOOL WINAPI RemoveDirectory(__in LPCTSTR lpPathName )用来删除文件夹。但是,如果文件夹中有文件或者子文件夹,该函数执行会失败。因此,必须使用递归方式删除子文件夹和文件。 以上代码实现此功能 |
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。