16,471
社区成员
发帖
与我相关
我的任务
分享
//
BOOL myDeleteDirectory(CString newPath)
{
CString search=newPath;
search +="\\*.*";
CFileFind finder;
BOOL bWorking = finder.FindFile(search);
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDirectory() && !finder.IsDots())
{//处理文件夹
//afxDump << finder.GetFilePath() << "\n";
myDeleteDirectory(finder.GetFilePath());
}
else
{//处理文件
//afxDump << finder.GetFilePath() << "\n";
DeleteFile(finder.GetFilePath());
}
}
return RemoveDirectory(newPath);
}
// del a fold
void CxxxxDlg::OnButton4()
{
// TODO: Add your control notification handler code here
CString root;
root = "APP";
BOOL ret=myDeleteDirectory(root);
if(!ret) AfxMessageBox(root+"Delete Failed!");
}
/**************************************************************************
DeleteFolder()
**************************************************************************/
BOOL DeleteFolder(HWND hWnd, LPSTR lpszFolder)
{
char szFile[MAX_PATH];
SHFILEOPSTRUCT fos;
WIN32_FIND_DATA FindData;
HANDLE hFind;
BOOL bFindFile = TRUE;
//we can't remove a directory that is not empty, so we need to empty this one
lstrcpy(szFile, lpszFolder);
lstrcat(szFile, "\\*.*");
ZeroMemory(&fos, sizeof(fos));
fos.hwnd = hWnd;
fos.wFunc = FO_DELETE;
fos.fFlags = FOF_SILENT | FOF_ALLOWUNDO; //send to the recycle bin
hFind = FindFirstFile(szFile, &FindData);
while((INVALID_HANDLE_VALUE != hFind) && bFindFile)
{
if(*(FindData.cFileName) != '.')
{
//copy the path and file name to our temp buffer
lstrcpy(szFile, lpszFolder);
lstrcat(szFile, "\\");
lstrcat(szFile, FindData.cFileName);
//add a second NULL because SHFileOperation is looking for this
lstrcat(szFile, "\0");
//delete the file
fos.pFrom = szFile;
SHFileOperation(&fos);
}
//find the next file
bFindFile = FindNextFile(hFind, &FindData);
}
FindClose(hFind);
return RemoveDirectory(lpszFolder);
}