Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C++
Article

Detecting if Recycle Bin is Empty

Rate me:
Please Sign up or sign in to vote.
2.69/5 (11 votes)
12 Mar 2008CPOL1 min read 27.7K   186   6   1
How to detect if recycle bin is empty or not? Find how in this article.

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); // windows memory management pointer needed later

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 :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer Plicatus Software and Microelectronic
Moldova (Republic of) Moldova (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy company Pin
Cojocaru Sergiu9-Nov-10 4:25
Cojocaru Sergiu9-Nov-10 4:25 
alcplus.md
http://www.alcplus.md

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.