Click here to Skip to main content
15,868,151 members
Articles / Desktop Programming / Win32
Tip/Trick

How to Delete a File or Folder to Recyclebin

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
20 Sep 2012MIT 26.9K   8   5
Delete a file or folder to Recyclebin using C#

Introduction

If we delete a file or folder using C# code, it will be deleted from the system permanently. Yesterday, I was searching for a solution, which would help me to delete a file to recycle bin. There is no direct API* available in .NET which could help me to achieve this. Later, I found an option using SHFileOperation function. SHFileOperation will be used to Copy, Move, Rename, or Delete a file system object. And here is the implementation which helped to delete file or folder to Recyclebin.

Using the Code

Given below is the implementation:

C#
[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)]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

public const int FO_DELETE = 3;
public const int FOF_ALLOWUNDO = 0x40;
public const int FOF_NOCONFIRMATION = 0x10;

And you can use WIN32 API like this, in the code, we are creating an instance of SHFILEOPSTRUCT structure. wFunc is the value that indicates which operation to perform. The pFrom is a pointer to one or more source file names. These names should be fully qualified paths to prevent unexpected results. fFlags control the file operation.

C#
var shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE; //Delete the files specified in pFrom.
shf.fFlags = FOF_ALLOWUNDO; //Preserve undo information, if possible. 
shf.pFrom = @"c:\myfile.txt" + '\0' + '\0'; //File / Folder to delete
SHFileOperation(ref shf);

The pFrom parameter can be either file or folder. * This is not 100% true. We can do this by using Microsoft.VisualBasic namespace. And here is the implementation.

C#
using Microsoft.VisualBasic; 

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

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Technical Lead
India India
Working as Tech. Lead

My blog : dotnetthoughts.net.
You can follow me in twitter : @anuraj

Comments and Discussions

 
Suggestionanother way Pin
S Stadler19-Nov-14 4:39
S Stadler19-Nov-14 4:39 
Simpler.

http://msdn.microsoft.com/en-us/library/ms127970(v=vs.100).aspx[^]

http://msdn.microsoft.com/en-us/library/ms127977(v=vs.90).aspx[^]
QuestionQuestion Pin
Bug.Maker21-Sep-12 0:24
Bug.Maker21-Sep-12 0:24 
AnswerRe: Question Pin
Anuraj Parameswaran21-Sep-12 1:51
Anuraj Parameswaran21-Sep-12 1:51 
GeneralMy vote of 5 Pin
Sivaprasad S Nair20-Sep-12 23:35
professionalSivaprasad S Nair20-Sep-12 23:35 
GeneralMy vote of 5 Pin
Vinod Viswanath20-Sep-12 22:55
Vinod Viswanath20-Sep-12 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.