C# Send Files or Folder to Recycle Bin

How do you do this Does .NET 2.0 have a built in function for handling this


Answer this question

C# Send Files or Folder to Recycle Bin

  • Preston Park

    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.


  • HarryBedi

    Hey soconne

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

    Byee and TC



  • Zclai

    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);


  • Avhacker

    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

  • LouArnold

    Thanks. I had actually just discovered that a little while ago. Thanks for your help guys.

  • ron nash

    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);


  • Dick Campbell

    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>

  • C# Send Files or Folder to Recycle Bin