ABOUT RECYCLE BIN
Recycle Bin is a feature of Microsoft Windows that allows you to send files and folders to a local "recycle bin", so that files can be restored before being deleted from the Windows system.
Microsoft released Recycle Bin with Windows 95, a long time ago.
Since Windows 95, nothing but the icon has really changed.
Windows 95

Windows 7

Windows 10
Initially, to delete files and folders programmatically you need to call an API from Shell32.dll.
DELETING TO RECYCLE BIN USING C#
Sadly, C# doesn't have a native API to delete files and folder to Recycle Bin.
BUT, Visual Basic HAS!!!
And you can call Visual Basic FROM C# as well.
THE CODE
So, let's go to the code.
Just copy and paste this code to a file named ExtensionDeleteToRecycleBin.cs or other name if you like.
- namespace System.IO
- {
- /// <summary>
- /// ExtensionDeleteToRecycleBin.cs
- /// </summary>
- public static class ExtensionDeleteToRecycleBin
- {
- /// <summary>
- /// Delete File To Recycle Bin
- /// WARMING: NETWORK FILES DON'T GO TO RECYCLE BIN
- /// </summary>
- /// <param name="file"></param>
- public static void FileRecycle(this string file)
- =>
- Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file,
- Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
- Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
- /// <summary>
- /// Delete Path To Recycle Bin
- /// WARMING: NETWORK PATHS DON'T GO TO RECYCLE BIN
- /// </summary>
- /// <param name="path"></param>
- public static void DirectoryRecycle(this string path)
- =>
- Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(path,
- Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
- Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
- }
- }
HOW TO USE
You need to make a reference to the namespace System.IO in your cs file.
For my use only, I just let on the namespace System, so it's easier to call it.
- using System;
- using System.IO;
- namespace AppDebugTests
- {
- class Program
- {
- [STAThread]
- static void Main(string[] args)
- {
- "C:\\MyFolder2Delete".DirectoryRecycle();
- "C:\\MyFile2Delete.txt".FileRecycle();
- Console.ReadKey();
- }
- }
- }
Another way to use it:
- var files = new[] { "File1.txt", "File2.txt" };
- foreach (var file in files)
- $"C:\\{file}".FileRecycle();
TAKE CARE
Files with more than 2GB and network resources don't go to the recycle bin!
Only local files can be sent to the recycle bin.
You can add an extension to delete forever by just changing a parameter:
Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently
- public static void DirectoryDelete4Ever(this string path) =>
- Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(path,
- Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
- Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently);
CONCLUSION
Deleting from the recycle bin is a way to avoid losing files, especially if your application processes original documents.
I'm in love with extension methods because we don't need to create variables to do something, we can do something directly from a const string.
Happy coding.

Andrew DennisonPosted Oct 7, 2021, 3:34 PM
Caveat: I use similar code, but it is not .NET Core compatible.
Andrew DennisonPosted Oct 7, 2021, 3:33 PM
I too use extensions methods, in my case too much. They are easier to type, but harder to find where they are implemented. They are static methods, and cannot be in an interface. Thus they cannot be easily mocked for testing or overridden for different environments, such as Linux. Finally, I argue that an extension method which takes a string should operate on the characters, not recycle a file, or send mail or any other side effect. Trust me I have some weird extensions like 10.Seconds(), which returns the timespan == 10 seconds. And can be used as 10.Seconds.Sleep(); static TimeSpan Seconds(this integer) is close to a type conversion and is equivalent to TimeSpan.FromSeconds() so it is hard to see how it might need to be overrriden. Sleep(TimeSpan) is fine as a static method, but probably should not be an extension method, just to save a little typing. If less typing is the goal, you can use static usings, like "using static System.Threading.Thread". Creating a method with the same signature in a different namespace is at best confusing. Depending on the combination of usings, you may get one method, or the other or a compilation error. Making it an extension method, allows the extension method to be found with the needed using, but is not the intention of extension methods.