Extension Methods For Deleting Files And Folders To Recycle Bin

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
 
Extension Methods For Delete Files And Folders To Recycle Bin
 
Windows 7
 
Extension Methods For Delete Files And Folders To Recycle Bin
 
Windows 10
 
Extension Methods For Delete Files And Folders To Recycle Bin
 
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.
  1. namespace System.IO  
  2. {  
  3.     /// <summary>  
  4.     /// ExtensionDeleteToRecycleBin.cs  
  5.     /// </summary>  
  6.     public static class ExtensionDeleteToRecycleBin  
  7.     {  
  8.   
  9.         /// <summary>  
  10.         /// Delete File To Recycle Bin  
  11.         /// WARMING: NETWORK FILES DON'T GO TO RECYCLE BIN  
  12.         /// </summary>  
  13.         /// <param name="file"></param>  
  14.         public static void FileRecycle(this string file)  
  15.             =>  
  16.         Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file,  
  17.             Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,  
  18.             Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);  
  19.   
  20.         /// <summary>  
  21.         /// Delete Path To Recycle Bin  
  22.         /// WARMING: NETWORK PATHS DON'T GO TO RECYCLE BIN  
  23.         /// </summary>  
  24.         /// <param name="path"></param>  
  25.         public static void DirectoryRecycle(this string path)  
  26.             =>  
  27.         Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(path,  
  28.             Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,  
  29.             Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);  
  30.   
  31.   
  32.     }  
  33. }  

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. 
  1. using System;  
  2. using System.IO;  
  3.   
  4. namespace AppDebugTests  
  5. {  
  6.     class Program  
  7.     {  
  8.         [STAThread]  
  9.         static void Main(string[] args)  
  10.         {  
  11.             "C:\\MyFolder2Delete".DirectoryRecycle();  
  12.   
  13.             "C:\\MyFile2Delete.txt".FileRecycle();  
  14.   
  15.             Console.ReadKey();  
  16.   
  17.         }  
  18.   
  19.     }  
  20. }  
Another way to use it: 
  1. var files = new[] { "File1.txt""File2.txt" };  
  2.   
  3. foreach (var file in files)              
  4.     $"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
  1. public static void DirectoryDelete4Ever(this string path) =>    
  2. Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(path,  
  3.     Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,  
  4.     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.