VC-如何删除回收站里的某指定文件?

VC-如何删除回收站里的某指定文件?

偏爱自由 发布于 2016-12-03 字数 102 浏览 1240 回复 1

微软提供了一个SHEmptyRecycleBin可以清空整个回收站,但是如果只想删除其中某一个文件,该怎么处理?

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2017-07-16 1 楼

void DeleteFileFromRecycleShell(const CString& sFileName)
{
HRESULT hr;

// Call this if needed.
CoInitialize( NULL );

IShellFolder* pRecycleFolder = NULL;
IMalloc* pMalloc = NULL;

hr = ::SHGetMalloc(&pMalloc);

// Get desktop folder.
hr = ::SHGetDesktopFolder(&pRecycleFolder);

// Get the recycleBin folder.
ITEMIDLIST* pidlRecycleFolder = NULL;
hr = ::SHGetSpecialFolderLocation(NULL, CSIDL_BITBUCKET, &pidlRecycleFolder);

// Get the IShellFolder of the recycleBin folder.
IShellFolder* pHistoryFolder = NULL;
hr = pRecycleFolder ->BindToObject(pidlRecycleFolder, NULL, IID_IShellFolder, (void**)&pRecycleFolder );

// Enumerate the recycleBin items.
IEnumIDList* pRecycleEnum = NULL;
hr = pRecycleFolder->EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pRecycleEnum );

ITEMIDLIST* pidl = NULL;
ULONG fetched = 0;

hr = pRecycleEnum ->Next(1, &pidl, &fetched);

STRRET strret;

if (SUCCEEDED(hr))
{
TCHAR pszPath[MAX_PATH]; // 保存路径
const ITEMIDLIST* pidl2 = pidl;
pisf->GetDisplayNameOf(idlCurrent,SHGDN_NORMAL,pszPath);

if(sFileName == pszPath)
{
// Get the IContextMenu interface.
IContextMenu* pContextMenu = NULL;
hr = pRecycleFolder->GetUIObjectOf(NULL, 1, &pidl2, IID_IContextMenu, NULL, (void**)&pContextMenu);

if (SUCCEEDED(hr))
{
CMINVOKECOMMANDINFO pCommandInfo = { 0 };

pCommandInfo.cbSize = sizeof(CMINVOKECOMMANDINFO);
pCommandInfo.lpVerb = _T("delete");
pCommandInfo.fMask = CMIC_MASK_FLAG_NO_UI; // has no effect
hr = pContextMenu->InvokeCommand(&pCommandInfo);
}

pContextMenu->Release();
}
}

pRecycleEnum ->Release();
pRecycleFolder->Release();
pMalloc->Release();
pidlRecycleFolder->Release();
}