Click here to Skip to main content
Licence CPOL
First Posted 12 Mar 2008
Views 11,038
Downloads 57
Bookmarked 6 times

Detecting if Recycle Bin is Empty

By | 12 Mar 2008 | Article
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)

About the Author

Cojocaru Sergiu

Systems Engineer
Plicatus Software and Microelectronic
Moldova (Republic Of) Moldova (Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy company PinmemberCojocaru Sergiu4:25 9 Nov '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Mar 2008
Article Copyright 2008 by Cojocaru Sergiu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid