The Win32 API to delete a directory is RemoveDirectory. But before using this, you will have to delete all the files under the directory and call RemoveDirectory API. If you’ve sub directories under it, it’s necesary to repeat the above steps and finally delete the root directory you’ve specified.
Wait, we’ve a painless API to do this, you don’t have to write code for recursive listing of directories and files while Shell has already provided an API to deal with file Operation.
You can call SHFileOperation as follows
[sourcecode language='cpp']
bool DeleteDirectory( CString strPath )
{
strPath += _T( ‘\ 0′ );
SHFILEOPSTRUCT strOper = { 0 };
strOper.hwnd = NULL;
strOper.wFunc = FO_DELETE;
strOper.pFrom = strPath;
strOper.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
if ( 0 == SHFileOperation ( &strOper ))
{
return true;
}
return false;
}
[/sourcecode]