#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();
清空();
}
|