Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Here is sample coding for how to empty the recycle bin using C#.

We can clear the recycle bin easily by using the Shell32.dll as follows.

shell32.dll

shell32.dll is a library which contains Windows Shell API functions, which are used when opening web pages and files.

First of all we need to create an enum as follows,

enum RecycleFlags : uint
        {
            SHERB_NOCONFIRMATION = 0x00000001,
            SHERB_NOPROGRESSUI = 0x00000002,
            SHERB_NOSOUND = 0x00000004
        }

And then, import the Shell32.dll using the DllImport class which is in System.Runtime.InteropServices namespaces. And creating the functionality SHEmptyRecycleBin as follows,

[DllImport("Shell32.dll",CharSet=CharSet.Unicode)]
        static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);

In my project I have two buttons like "Clear the RecycleBin" and "Exit".

When clicking the Clear the RecycleBin, we have to clear the recyclebin. So the following code will help for do that function. Copy the following code in to that button clicking event.

try
 {
   uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);
   MessageBox.Show(this,"Done !","Empty the 
          RecycleBin",MessageBoxButtons.OK,MessageBoxIcon.Information);
 }
catch(Exception ex)
  {
    MessageBox.Show(this, "Failed ! " + ex.Message, "Empty the 
RecycleBin", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    Application.Exit();
  }

Exit the application when click the "Exit" button.

Application.Exit();

Now run the program,

Click the "Clear the Recycle Bin"

The confirmation window will appear for deleting all items for delete all items in the Recycle Bin.

Give yes to delete all items from Recycle Bin.

Yes�that's it...

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralEmpty Recycle Bin on 64 bit OS
Carl Lockett
12:53 30 Dec '09  
How can I modify the above code to work on Windows 7/Vista 64 bit operating systems? The code works great with all 32 bit OS's, but I can't get it to work with 64 bit. Please help!
GeneralMy vote of 1
VMykyt
21:55 16 Jul '09  
poor
GeneralHi
DharmarajNagarajan
20:52 18 Feb '09  
To empty the Recycle bin without confirmation.
public uint EmptyRecycleBin()
{
uint result;

try
{
result = SHEmptyRecycleBin(IntPtr.Zero,null,RecycleFalgs.SHERB_NOCONFIRMATION );
}
catch (AccessViolationException ax)
{
result = 0;
Console.WriteLine(ax.Message);
}
return result;
}
QuestionRecycleFlags
xidar
5:52 24 Aug '07  
wondering why you define RecycleFlags but don't use them in your own code?
GeneralInteresting Idea
merlin981
5:29 24 Aug '07  
Although, I'm not sure why you would want to force the application to exit if the recycle bin failed to empty (see Catch block).


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I get my developer tools from Merlin A.I. Soft
Rhabot - World of Warcraft Bot
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Last Updated 24 Aug 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010