C# Tips

C# Tip Article

How to empty Recycle Bin in C#?

.NET Framework does not have handy way of emptying the Recycle Bin files. But this task can be done easily by using Shell Win32 API.

To empty the Recycle Bin, simply call SHEmptyRecycleBin() function in shell32.dll as shown below.

class Program
{
	static void Main(string[] args)
	{
	    // Do not show confirm message box
		const int SHERB_NOCONFIRMATION = 0x00000001;
		
		// empty Recycle Bin
		SHEmptyRecycleBin(IntPtr.Zero, null, SHERB_NOCONFIRMATION);
	}

	[DllImport("shell32.dll")]
	static extern int SHEmptyRecycleBin(IntPtr hWnd, string pszRootPath, uint dwFlags);
}

The 3rd parameter (SHERB_NOCONFIRMATION)in SHEmptyRecycleBin() keeps confirmation message box from showing up. If you want to show confirm message box, simply put 0 in the 3rd parameter.