Click here to Skip to main content
11,435,048 members (49,375 online)
Click here to Skip to main content

Tagged as

Detecting if Recycle Bin is Empty

, 12 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) )) 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 Smile | :)

License

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

Share

About the Author

Cojocaru Sergiu
Systems Engineer Plicatus Software and Microelectronic
Moldova (Republic Of) Moldova (Republic Of)
No Biography provided

Comments and Discussions

 
GeneralMy company Pin
Cojocaru Sergiu9-Nov-10 5:25
memberCojocaru Sergiu9-Nov-10 5:25 

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.

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