Disable Windows Recycle Bin delete confirmation in C#

-1

I am creating a small program which has a few functionalities to make my PC "feel better". One of those functionalities is emptying my recycle bin.

I am using the following code for that:

enum RecycleFlags : uint
{
    SHERB_NOCONFIRMATION = 0x00000001, // No empty confirmation
    SHERB_NOPROGRESSUI = 0x00000002, // No progress tracking
    SHERB_NOSOUND = 0x00000004 // No sound on completion
}

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

private bool IsEmpty()
{
    Shell shell = new Shell();
    Folder recycleBin = shell.NameSpace(10);

    if (recycleBin.Items().Count == 0)
    {
        return true;
    }
    return false;
}

public bool Empty()
{
    if (IsEmpty() || SHEmptyRecycleBin(IntPtr.Zero, null, 0) == 0)
    {
        return true;
    }
    return false;
}

So in another class I am using these functions. I first check if the recycle bin has any items, before we start deleting them. If yes, then delete them. If no, then don't do anything.

However the following happens: If I have several items to delete, let's say 4. Before the program starts deleting them, a Windows messagebox pops up asking me if I'm sure that I want to delete 4 items. That causes some user interaction in a process that should be fully automated after activation.

I have done some research, but it seems like I can only deactivate that confirmation message on my own pc. Within the recycle bin properties. I do know that CCleaner for example does empty the recycle bin aswell, without a confirmation popup. So it should be possible.

How can I "skip" this confirmation step in code? I've tried some administrator privileges within the manifest, but it didn't work.

Here is an example of the messagebox: enter image description here

If skipping the messagebox is not an option, I would like to know if there is a way to automatically answer the question? If so, I want the program to answer "Yes". Because if the user presses "No", the SHEmptyRecycleBin function will still return a "success" value, while the items are not removed. So I will have to do a "count" check again after the empty function.

c#
windows
recycle-bin
shell32
asked on Stack Overflow Aug 23, 2020 by Cihan Kurt • edited Aug 23, 2020 by Robert Harvey

1 Answer

0

I forgot to pass the right flags in the function call.

I called the function with this:

SHEmptyRecycleBin(IntPtr.Zero, null, 0)

While it should be this:

SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOCONFIRMATION | RecycleFlags.SHERB_NOPROGRESSUI | RecycleFlags.SHERB_NOSOUND)

Credits go to Cid, for answering my question in the comments. Thank you.

answered on Stack Overflow Aug 23, 2020 by Cihan Kurt

User contributions licensed under CC BY-SA 3.0