Click here to Skip to main content
6,595,444 members and growing! (17,518 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Empty the Recycle Bin using C#.

By S.Vinothkumar

Empty the Recycle Bin using C#.
C# 2.0, Windows, .NET 2.0, WinForms, VS2005, Dev
Posted:24 Aug 2007
Views:16,622
Bookmarked:14 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.06 Rating: 2.44 out of 5
2 votes, 28.6%
1
2 votes, 28.6%
2
1 vote, 14.3%
3
1 vote, 14.3%
4
1 vote, 14.3%
5

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...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

S.Vinothkumar


Member
Hi Viewers,

I wish to all. This is Vinoth. This is where I try to condense everything that you need to know about me.

Blog:

visit my blog

Interests:

I'm passionate about a great many things and continually learning about the things that interest me. They are wearable computers, User Interface Design, Artificial life, Industrial music.






Occupation: Web Developer
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberVMykyt21:55 16 Jul '09  
GeneralHi PinmemberDharmarajNagarajan20:52 18 Feb '09  
QuestionRecycleFlags Pinmemberxidar5:52 24 Aug '07  
GeneralInteresting Idea Pinmembermerlin9815:29 24 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Aug 2007
Editor:
Copyright 2007 by S.Vinothkumar
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project