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:
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.
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.
User contributions licensed under CC BY-SA 3.0