Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 535
xmllmx Avatar asked Mar 22 '23 16:03

xmllmx


2 Answers

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

like image 113
Sudhakar Tillapudi Avatar answered Apr 25 '23 00:04

Sudhakar Tillapudi


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
        }
    }
}
like image 27
Ivan Kishchenko Avatar answered Apr 25 '23 01:04

Ivan Kishchenko