#include <afxwin.h>
//参数:要移除的目录的路径
bool DeleteDirectory(char* DirName)
{
char tempFileFind[MAX_PATH];
sprintf( tempFileFind, "%s\\*.*", DirName );
CFileFind tempFind;
BOOL IsFinded = tempFind.FindFile(tempFileFind);
while ( IsFinded )
{
IsFinded = tempFind.FindNextFile();
if ( !tempFind.IsDots() )
{
char foundFileName[MAX_PATH];
strcpy( foundFileName, tempFind.GetFileName().GetBuffer(MAX_PATH) );
if ( tempFind.IsDirectory() )
{
char tempDir[MAX_PATH];
sprintf( tempDir, "%s\\%s", DirName, foundFileName );
DeleteDirectory( tempDir ); //递归调用自身
}
else
{
char tempFileName[MAX_PATH];
sprintf( tempFileName, "%s\\%s", DirName, foundFileName );
DeleteFile( tempFileName );
}
}
}
tempFind.Close();
if( !RemoveDirectory(DirName) )
{
return false;
}
return true;
}
int main(void)
{
//删除D盘根目录下的一个名为hero的目录
DeleteDirectory("D:\\hero\\");
return 0;
}