Introduction
I decided to write this code 'cause saw a lot of questions on detecting if a recycle bin is empty or not ( not just empty it, but detect if it has files or not). Source code is a COM component that will be helpfull for "Real Coders in C#".
Background
If you are C++ coder you must know Shell Programming and what shlguid.h and shlobj.h contain. If you are c# coder.. hmmm what is shell, and pointers :))) In fact I created COM to make easies C# coders to import this code in their application without any PInvoke, etc...
Using the code
Coders in C++ use it as you wish: use COM or just copy "IsEmpty" function from RecycleBinOps ( do not forget to modify return value from HRESULT to BOOL). Coders in C#: add reference -> Select Com tab -> select "CommShell v1.0" component and press OK -> write in your code: "using CommShellLib" and in your method add something like this:
<code>
IRecycleBinOps recBin = new RecycleBinOps();
if (recBin.IsEmpty() == 1)
return true;
else
return false;
</code>
In fact function source looks in this way (so you may not download source):
<CODE>
BOOL bResult = FALSE;
LPSHELLFOLDER pDesktop = NULL;
LPITEMIDLIST pidlRecycleBin = NULL;
HRESULT hr = S_OK;
LPSHELLFOLDER m_pRecycleBin;
LPENUMIDLIST penumFiles = NULL;
LPITEMIDLIST pidl = NULL;
LPMALLOC pMalloc = NULL;
hr = SHGetDesktopFolder(&pDesktop);
hr = SHGetSpecialFolderLocation (NULL, CSIDL_BITBUCKET, &pidlRecycleBin);
hr = pDesktop->BindToObject(pidlRecycleBin, NULL, IID_IShellFolder,
(LPVOID *)&m_pRecycleBin);
SHGetMalloc(&pMalloc);
hr = m_pRecycleBin->EnumObjects(NULL,
SHCONTF_FOLDERS|SHCONTF_NONFOLDERS| SHCONTF_INCLUDEHIDDEN,
&penumFiles);
if(SUCCEEDED (hr))
{
while(penumFiles->Next(1, &pidl, NULL) != S_FALSE)
{
bResult = TRUE;
break;
}
}
if (NULL != penumFiles)
{
penumFiles->Release ();
penumFiles = NULL;
}
pMalloc->Release();
return bResult;
</CODE>
Idea is very simple: We obtain pointer to recycle bin using SHGetSpecialFolderLocation and after this we are starting to enumerate objects from this pointer. All information is taken from MSDN where this function are described very well. I just used them in direction I need.
History
Ver 1.0 :)