xmllmx
xmllmx

Reputation: 42379

How to programatically clear the recycle bin under Windows?

In case I want to programatically clear the recycle bin under Windows, how to implement that?

Does IFileOperation help?

Upvotes: 5

Views: 3185

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

You can use SHEmptyRecycleBin() function from shell32.dll library to achieve this.

Upvotes: 5

Ivan Kishchenko
Ivan Kishchenko

Reputation: 815

Full example:

using System;
using System.Runtime.InteropServices;

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

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

    static void Main(string[] args)
    {
        uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);
        if (result == 0)
        {
            //OK
        }
    }
}

Upvotes: 4

Related Questions