一句代碼清空回收站
#include <shlobj.h>
#include <shlwapi.h>
#include <iostream>
#pragma comment(lib,"Shell32.lib")
DWORD ListFileInRecycleBin(){
CHAR pszPath[MAX_PATH]; // 保存路徑
// IShellFolder接口
IShellFolder *pisf = NULL;
IShellFolder *pisfRecBin = NULL;
// 獲取“根”目錄,桌面
SHGetDesktopFolder(&pisfRecBin);
IEnumIDList *peidl = NULL; // 對象遍歷接口
LPITEMIDLIST pidlBin = NULL;
LPITEMIDLIST idlCurrent = NULL;
LPMALLOC pMalloc = NULL;
// 分配
SHGetMalloc(&pMalloc);
// 回收站位置
SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidlBin);
// 綁定回收站對象
pisfRecBin->BindToObject(pidlBin,NULL,IID_IShellFolder,(void **) &pisf);
// 列舉回收站中的對象,得到IEnumIDList接口,包括SHCONTF_FOLDERS、
// SHCONTF_NONFOLDERS、SHCONTF_INCLUDEHIDDEN類型的對象
pisf->EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS |SHCONTF_INCLUDEHIDDEN, &peidl);
STRRET strret;ULONG uFetched;
HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
printf("\n哪些文件在回收站:\n");
while(1)
{
// 遍歷IEnumIDList對象,idlCurrent爲當前對象
if(peidl->Next(1,&idlCurrent,&uFetched) == S_FALSE)
break;
// 獲取回收站當前對象當前的路徑,這裏沒有輸出結果,讀者可自行修改
SHGetPathFromIDList(idlCurrent, pszPath);
// DisplayName,刪除前的路徑
pisf->GetDisplayNameOf(idlCurrent,SHGDN_NORMAL,&strret);
// 顯示,printf可能會造成字符編碼不正確。
WriteConsoleW(hOutPut,strret.pOleStr,lstrlenW(strret.pOleStr),NULL,NULL);
WriteConsoleW(hOutPut,L"\n", 1, NULL, NULL);
//DeleteFile(strret.cStr);
//
}
// 釋放資源
pMalloc->Free(pidlBin);
pMalloc->Free(strret.pOleStr);
pMalloc->Release();
peidl->Release();
pisf->Release();
return 0;
}
void 清空(){//這纔是關鍵
SHEmptyRecycleBin(NULL,NULL,SHERB_NOCONFIRMATION|SHERB_NOPROGRESSUI|SHERB_NOSOUND);
}
void main(){
//ListFileInRecycleBin();
清空();
}