29

Programmatic solution of course...

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
  • 1
    Possible duplicate of [Send a File to the Recycle Bin](http://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin) – MicroVirus Jun 21 '16 at 10:56

3 Answers3

38

http://www.daveamenta.com/2008-05/c-delete-a-file-to-the-recycle-bin/

From above:

using Microsoft.VisualBasic;

string path = @"c:\myfile.txt";
FileIO.FileSystem.DeleteDirectory(path, 
    FileIO.UIOption.OnlyErrorDialogs, 
    RecycleOption.SendToRecycleBin);
Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
TK.
  • 46,577
  • 46
  • 119
  • 147
  • I'd use DeleteFile instead of DeleteDirectory to be more clear. – scobi Dec 19 '08 at 03:59
  • 8
    +1 for thinking outside the box and referencing a disliked namespace rather than resorting to ugly unmanaged code. – BenAlabaster Dec 22 '08 at 15:26
  • 4
    What do these FileIO classes and methods have to do with Visual Basic? Putting them in Microsoft.VisualBasic makes absolutely no sense to me. I must be missing something. – I. J. Kennedy Feb 21 '11 at 20:20
  • 1
    @I. J. Kennedy If I had to guess it might have been that the Visual basic team implanted the feature and rather than muck around with it the .NET team decided to leave the functionality where it was. – TK. Feb 21 '11 at 20:29
  • 4
    any solution without using "Microsoft.VisualBasic" ? – Kiquenet Apr 06 '11 at 07:56
  • Is there any way to not show the UI at all? (i.e. raise an exception instead of showing an error dialog) – Thomas Levesque Feb 13 '13 at 10:38
  • Yeah I'm looking for one without having to use the Visual Basic DLL – puretppc Jan 25 '14 at 04:42
18

You need to delve into unmanaged code. Here's a static class that I've been using:

public static class Recycle
{
    private const int FO_DELETE = 3;
    private const int FOF_ALLOWUNDO = 0x40;
    private const int FOF_NOCONFIRMATION = 0x0010;

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

    public static void DeleteFileOperation(string filePath)
    {
        SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
        fileop.wFunc = FO_DELETE;
        fileop.pFrom = filePath + '\0' + '\0';
        fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;

        SHFileOperation(ref fileop);
    }
}

Addendum:

  • Tsk tsk @ Jeff for "using Microsoft.VisualBasic" in C# code.
  • Tsk tsk @ MS for putting all the goodies in VisualBasic namespace.
Ishmaeel
  • 14,138
  • 9
  • 71
  • 83
  • 12
    I guess this is one of the areas where VB is just better than C#...besides, it's not "not C#" just because it's in the VB namespace - an object is an object, not to use it just because it's in a namespace you don't like the name of is a little ridiculous don't you think?... – BenAlabaster Dec 22 '08 at 15:18
  • 10
    ...if the namespace was Microsoft.UsefulUtilities you wouldn't have an reservations about using them, so what's the difference? – BenAlabaster Dec 22 '08 at 15:18
  • 3
    It's not about the namespace of course, but that you have to link in VB libraries to use that namespace. Should be obvious. – Mike Lischke Apr 28 '13 at 09:36
13

The best way I have found is to use the VB function FileSystem.DeleteFile.

Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file.FullName,
    Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
    Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);

It requires adding Microsoft.VisualBasic as a reference, but this is part of the .NET framework and so isn't an extra dependency.

Alternate solutions require a P/Invoke to SHFileOperation, as well as defining all the various structures/constants. Including Microsoft.VisualBasic is much neater by comparison.

Zooba
  • 11,221
  • 3
  • 37
  • 40