Tag Archives: SHFileOperation
Delete files or folders to recycle bin in C#
Sadly C# doesn’t have the required libraries in place to aid the users or the developers to send files or folders directly to the recycle bin.
System.IO.Directory.Delete() simply delete the path without allowing for the action to be undone.
Luckily there are two ways this can be resolved. Either by using the Windows API
SHFileOperation function or by referencing
Microsoft.VisualBasic and using the
Personally I prefer the
Microsoft.VisualBasic approach as the code looks much cleaner and is more readable to me but I will demonstrate how both methods can be implemented either way.
The Microsoft.VisualBasic approach:
Microsoft.VisualBasic in your project and simply call
depending if you want to delete a file or a folder.
Posted in C#.
Tagged C#, csharp, delete, delete file, delete folder, SHFileOperation, SHFILEOPSTRUCT, snippet, winforms