Eric Bergman-Terrell's Blog

.NET Programming Tip: How to Programmatically Send Folders and/or Files to the Recycle Bin
October 4, 2010

Use the SHFileOperation API to send a file or folder to the Recycle Bin. In the SHFILEOPSTRUCT that you pass to SHFileOperation, set the pFrom field to a string containing each file or folder path seperated by a null character. Make sure that there is an additional null character at the end of the pFrom string. This code is adapted from source code written by Jeff Key (author of Snippet Compiler).

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


fileop.wFunc = FO_DELETE;
fileop.pFrom = filePath + '\0' + '\0';

SHFileOperation(ref fileop);
Keywords: Recycle Bin, SHFileOperation, SHFILEOPSTRUCT

Reader Comments

Comment on this Blog Post

Recent Posts

EBTCalc Users: A Desktop Version is Now Available!October 25, 2019
Python Script to Audit MediaMonkey TranscodingAugust 15, 2019
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016