C#: Delete a file to the recycle bin

May 5, 2008 by Dave
 

I had a friend ask me yesterday what the easiest/best way to delete a file to the recycle bin was, in C# that is.  I knew about SHFileOperation, but I didn’t know if .NET 2.0 had native support for sending files to the recycle bin.  As it turns out, there are two ways in which you can move files to the recycle bin.  Microsoft has included the support in the Microsoft.VisualBasic namespace, and referencing the Microsoft.VisualBasic assembly will allow you to use that function directly.

Deleting a file using Managed code in C# or VB.NET:

(Remember to add a reference to Microsoft.VisualBasic)

using Microsoft.VisualBasic;
 
string path = @"c:\myfile.txt";
FileIO.FileSystem.DeleteDirectory(path, FileIO.UIOption.OnlyErrorDialogs, .RecycleOption.SendToRecycleBin);

The other alternative is to use COM Interop and P/Invoke the SHFileOperation from the Windows shell32.dll.

Deleting a file using P/Invoke in C#:

I have written a class that provides methods to easily specify how the file should be sent to the recycle bin.  SHFileOperation can take flags, which I have put into an enum and commented.  These flags define how the operation should be executed.

  • FOF_SILENT – Do not show the “Recycling…” dialog.
  • FOF_NOCONFIRMATION – Do not prompt the use for confirmation of the operation.
  • FOF_ALLOWUNDO – This is the option that specifies that the file should be sent to the recycle bin.  It is added by default in each Send method.
  • FOF_SIMPLEPROGRESS – This option will surpress the display of the names of the files that are being deleted.
  • FOF_NOERRORUI – This option will suppress any errors encountered during the operation.
  • FOF_WANTNUKEWARNING – Adding this flag will allow a dialog to allow the user to cancel removing large files permanently, if they are too large for the recycle bin.

Any one of these flags can be added to change the operation.

Methods provided by the RecycleBin class:

  • bool Send(string path)
  • bool Send(string path, FileOperationFlags flags)
  • static bool SendSilent(string path)

The first will simply send a path to the bin while showing the dialog, but surpressing the confirmation.

The second will allow you to customize the flags for the file operation, the only flag set for you is FOF_ALLOWUNDO. Flags can be combined with the | operator. e.g. FOF_NOERRORUI | FOF_SIMPLEPROGRESS.

The third, and probably most commonly used, will surpress all errors and warnings, and not show the user any operation is in progress.

Code Sample:

SendToRecycleBin.zip [ZIP] [Visual Studio 2008] (43KB)

Update – 04/18/2010:  Updated sample for x64 compatibility.

Comments

7 Comments on C#: Delete a file to the recycle bin

    [...] ShowWindow is an example of an API that has been wrapped nicely by the .NET CLR, it is unlikely that you will ever need to import and call it.  But something more useful might be: Deleting a file to the recycle bin. [...]

  1. Abdullah Battal on Wed, 28th May 2008 3:45 am
  2. The code works, but it doesn’t give us a chance to understand if the user changes his mind when the delete confirmation box appears. For example; it’s not possible to change a label’s text as “file deleted” or “operation cancelled” according to the user’s response to the confirmation dialog. Or, at least, I couldn’t do it… :/
    RecybleBin.Send always returns true.

  3. Dave on Sat, 7th Jun 2008 12:50 pm
  4. That is correct – you will need to check that the files are gone from your source location before you continue. Not only would that be good practice either way, but I think it would be the best way to detect errors that could come from other places.

  5. How to roll back file delete operation - bytes on Tue, 24th Feb 2009 11:59 am
  6. [...] Hi, Please refer the following link for your requirement: http://www.daveamenta.com/2008-05/c-…e-recycle-bin/ [...]

  7. Pete Souza on Sat, 16th May 2009 1:17 am
  8. Nice, but doesn’t seem to work under Windows 7 x64. File is just never deleted (full Admin permissions).

  9. MdBoy on Fri, 26th Feb 2010 1:59 pm
  10. I know it’s been long time since release but worth mentioning that to fix the code for 64bit you need to remove Pack = 1 from struct.

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]

    Reason:
    Under x64, the SHFILEOPSTRUCT must be declared without the Pack = 1 parameter, or it will fail. This is a real pain if you want your code to be platform independent, as you have to declare two separate structures, one with Pack = 1, and one without. You then have to declare two different SHFileOperation calls, one for each of the structures. Then you have to decide which one to call depending on whether you are running on 32 or 64 bit.

  11. Dave on Fri, 26th Feb 2010 4:06 pm
  12. @MdBoy – Thanks for pointing that out! When I wrote this little example, I wasn’t using an x64 operating system, and did not consider that. I will go back and update this article to be platform independent.