BOOL DeleteDirectory(LPCTSTR DirName)
{
CFileFind tempFind; //聲明一個CFileFind類變量,以用來搜尋
char tempFileFind[200]; //用于定義搜尋格式
sprintf(tempFileFind,"%s\\*.*",DirName);
//比對格式為*.*,即該目錄下的所有檔案
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
//查找第一個檔案
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile(); //遞歸搜尋其他的檔案
if(!tempFind.IsDots()) //如果不是"."目錄
{
char foundFileName[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory()) //如果是目錄,則遞歸地調用
{
//DeleteDirectory
char tempDir[200];
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DeleteDirectory(tempDir);
}
else
{
//如果是檔案則直接删除之
char tempFileName[200];
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(DirName)) //删除目錄
{
AfxMessageBox("删除目錄失敗!",MB_OK);
return FALSE;
}
return TRUE;
}