📜 ⬆️ ⬇️

How do I know that the directory is actually a recycle bin?

Here is a question caused by a real customer request:

I need a function that determines by its path whether it is part of the Recycle Bin. I tried to use SHGetSpecialFolderPath with CSIDL_BITBUCKET , but this does not work because the Recycle Bin is a virtual directory that is a combination of SHGetSpecialFolderPath CSIDL_BITBUCKET from all disks.

The client writes that he does not want to hard-write (hard code) the words RECYLCED and RECYCLER - a good solution, since these names depend on a lot. I already wrote that it depends on the file system . It also depends on whether the disk is accessed locally or remotely: the basket accessible via the network is called differently. This may depend on the operating system that is installed on the user. No, it’s a bad idea to prescribe the folder names for the recycle bin in the program.
')
The SHDESCRIPTIONID structure reports a bit more about the shell folder. In addition to the “description ID”, it also gives the CLSID , and this is what is important in this case.

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>
#include <stdio.h>

HRESULT
GetFolderDescriptionId(LPCWSTR pszPath, SHDESCRIPTIONID *pdid)
{
HRESULT hr;
LPITEMIDLIST pidl;
if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL,
&pidl, 0, NULL))) {
IShellFolder *psf;
LPCITEMIDLIST pidlChild;
if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder,
(void**)&psf, &pidlChild))) {
hr = SHGetDataFromIDList(psf, pidlChild,
SHGDFIL_DESCRIPTIONID, pdid, sizeof(*pdid));
psf->Release();
}
CoTaskMemFree(pidl);
}
return hr;
}

int __cdecl wmain(int argc, WCHAR **argv)
{
SHDESCRIPTIONID did;
if (SUCCEEDED(GetFolderDescriptionId(argv[1], &did)) &&
did.clsid == CLSID_RecycleBin) {
printf("is a recycle bin\n");
} else {
printf("is not a recycle bin\n");
}
return 0;
}


The GetFolderDescriptionId function takes the path to the folder and turns it into ITEMIDLIST to call SHGetDataFromIDList and get the SHDESCRIPTIONID . All we care about is whether CLSID is Baskets or not.

C:\> checkrecycle C:\Windows
is not a recycle bin
C:\> checkrecycle C:\RECYCLER\S-1-5-21-2127521184-1604012920-1887927527-72713
is a recycle bin


Of course, after I said how to do it, I have to say how not to do it . This is another example of how a client has a problem, he solves half and asks for advice on how to solve the other , without noticing that he approaches the problem from the wrong side. Next time we look at the real problem of the client.

Source: https://habr.com/ru/post/41123/


All Articles