Using the Shell API to Delete a Directory

Using the Shell API to Delete a Directory

use 'SHFileOperation', which copies, removes, renames or deletes a file system objec


#include <shellapi.h>
 
bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true)
{
	int len = _tcslen(lpszDir);
	TCHAR *pszFrom = new TCHAR[len+2];
	_tcscpy(pszFrom, lpszDir);
	pszFrom[len] = 0;
	pszFrom[len+1] = 0;
	 
	SHFILEOPSTRUCT fileop;
	fileop.hwnd   = NULL;    // no status display
	fileop.wFunc  = FO_DELETE;  // delete operation
	fileop.pFrom  = pszFrom;  // source file name as double null terminated string
	fileop.pTo    = NULL;    // no destination needed
	fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user
	 
	if(!noRecycleBin)
	fileop.fFlags |= FOF_ALLOWUNDO;
	 
	fileop.fAnyOperationsAborted = FALSE;
	fileop.lpszProgressTitle     = NULL;
	fileop.hNameMappings         = NULL;
	 
	int ret = SHFileOperation(&fileop);
	delete [] pszFrom;
	return (ret == 0);
}
 
int main()
{
	DeleteDirectory("d:\\Test", false);
	return 0;
}

 

Dotblogs 的標籤: , ,