Answered C# Send Files or Folder to Recycle Bin

  • Monday, December 04, 2006 6:20 PM
     
     
    How do you do this?  Does .NET 2.0 have a built in function for handling this?

All Replies

  • Monday, December 04, 2006 7:08 PM
     
     Proposed
    No built in functions that I can find, but a quick google search turns up this post.

    For reference:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
            public struct SHFILEOPSTRUCT
    {
            public IntPtr hwnd;
            [MarshalAs(UnmanagedType.U4)] public int wFunc;
            public string pFrom;
            public string pTo;
            public short fFlags;
            [MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
            public IntPtr hNameMappings;
            public string lpszProgressTitle;

    }

    [DllImport("shell32.dll", CharSet=CharSet.Auto)]
    static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
    const int FO_DELETE = 3;
    const int FOF_ALLOWUNDO = 0x40;
    const int FOF_NOCONFIRMATION = 0x10;    //Don't prompt the user.;

    To use the API:

    SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
    shf.wFunc = FO_DELETE;
    shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
    shf.pFrom = "C:\\test.txt";

    SHFileOperation(ref shf);
    • Proposed As Answer by Gary Le Sueur Monday, October 12, 2009 10:21 AM
    •  
  • Monday, December 04, 2006 7:09 PM
    Moderator
     
     Answered
    To do this in C# 2.0 do these steps:
    1. Add a reference to Microsoft.VisualBasic assembly. The needed class is found in this library.
    2. Add this using statement to the top of the file using Microsoft.VisualBasic.FileIO;
    3. Use FileSystem.DeleteFile to delete a file, it has the option to specify recycle bin or not.
    4. Use FileSystem.DeleteDirectory to delete a directory with the option to specify to send it to the recycle bin or not.
    If you are in VB.Net the need to reference the VB assembly is not required, just reference the namespace.
  • Tuesday, December 05, 2006 4:48 AM
     
     
    Thanks.  I had actually just discovered that a little while ago.  Thanks for your help guys.
  • Tuesday, December 05, 2006 10:41 AM
     
     

    Hey soconne

    Can u send the code that u have used to delete a file and put it in Recyclebin,

    Byee and TC

  • Tuesday, December 05, 2006 2:52 PM
    Moderator
     
     
    Prashant: MSDN has an example of the deleting to the recycle bin  How to: Delete a File in Visual Basic ... also remember one can F1 on any item in Visual Studio editor to be taken to help.<br>
  • Tuesday, December 05, 2006 4:42 PM
     
     
    Here's what I did, add Microsoft.VisualBasic as a reference and then do:

    Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(filename or folder,
                                                                  Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
                                                                    Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);

  • Thursday, December 07, 2006 11:42 AM
     
     
    Is in VB using Framework 2.0 with Visual Studio 2005 but should not be a problem to convert it to C#

    Delete File:
    My.Computer.FileSystem.DeleteFile("THE FILE TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

    Delete Directory:
    My.Computer.FileSystem.DeleteDirectory("THE DIRECTORY TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

    Have fun and be carefull.

    Tony
  • Tuesday, November 10, 2009 4:59 PM
     
     
    Note that this line:
    shf.pFrom = "C:\\test.txt";

    Is wrong - pFrom must be double null terminated.

    shf.pFrom = "C:\\text.txt\0"; is correct as per

    http://msdn.microsoft.com/en-us/library/bb759795%28VS.85%29.aspx
  • Friday, February 19, 2010 1:49 PM
     
     
    @Michael Letterle

    Thanks it works fine. 
    But the only problem I have is that the file in the trash as no name. Is there any solution to fix this issue?

    Thanks in advance
  • Thursday, January 20, 2011 12:39 PM
     
     
    To do this in C# 2.0 do these steps:
    1. Add a reference to Microsoft.VisualBasic assembly. The needed class is found in this library.
    If you are in VB.Net the need to reference the VB assembly is not required, just reference the namespace.

    I think there is a (good) reason why Microsoft has implemented this under VisualBasic namespace ... It is not a good practice to (ab)use it in such situations. If MS would like to use that it would have been implemented in another namespace (e.g. Microsoft.Windows.Shell or whatever ^^)

    You should use a "shell operation" as suggested by "Michael Letterle"!


    Programming is a kind of art but not all programmers are artists.
  • Thursday, June 30, 2011 7:00 PM
     
     
    This is because it was implemented to allow Visual Basic 6 code to work in Visual Basic.NET, hence why it is in the VisualBasic namespace. It in no way means you should not use it - after all, all it does is wrap a shell operation.
  • Friday, November 23, 2012 12:29 AM
     
     
    tested, it works! Tks a lot

    Phuong