Advertisement
ivandrofly

Send file to recycle bin

Jan 22nd, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1. public class FileOperationAPIWrapper
  2.     {
  3.         /// <summary>
  4.         /// Possible flags for the SHFileOperation method.
  5.         /// </summary>
  6.         [Flags]
  7.         public enum FileOperationFlags : ushort
  8.         {
  9.             /// <summary>
  10.             /// Do not show a dialog during the process
  11.             /// </summary>
  12.             FOF_SILENT = 0x0004,
  13.             /// <summary>
  14.             /// Do not ask the user to confirm selection
  15.             /// </summary>
  16.             FOF_NOCONFIRMATION = 0x0010,
  17.             /// <summary>
  18.             /// Delete the file to the recycle bin.  (Required flag to send a file to the bin
  19.             /// </summary>
  20.             FOF_ALLOWUNDO = 0x0040,
  21.             /// <summary>
  22.             /// Do not show the names of the files or folders that are being recycled.
  23.             /// </summary>
  24.             FOF_SIMPLEPROGRESS = 0x0100,
  25.             /// <summary>
  26.             /// Surpress errors, if any occur during the process.
  27.             /// </summary>
  28.             FOF_NOERRORUI = 0x0400,
  29.             /// <summary>
  30.             /// Warn if files are too big to fit in the recycle bin and will need
  31.             /// to be deleted completely.
  32.             /// </summary>
  33.             FOF_WANTNUKEWARNING = 0x4000,
  34.         }
  35.  
  36.         /// <summary>
  37.         /// File Operation Function Type for SHFileOperation
  38.         /// </summary>
  39.         public enum FileOperationType : uint
  40.         {
  41.             /// <summary>
  42.             /// Move the objects
  43.             /// </summary>
  44.             FO_MOVE = 0x0001,
  45.             /// <summary>
  46.             /// Copy the objects
  47.             /// </summary>
  48.             FO_COPY = 0x0002,
  49.             /// <summary>
  50.             /// Delete (or recycle) the objects
  51.             /// </summary>
  52.             FO_DELETE = 0x0003,
  53.             /// <summary>
  54.             /// Rename the object(s)
  55.             /// </summary>
  56.             FO_RENAME = 0x0004,
  57.         }
  58.  
  59.  
  60.  
  61.         /// <summary>
  62.         /// SHFILEOPSTRUCT for SHFileOperation from COM
  63.         /// </summary>
  64.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
  65.         private struct SHFILEOPSTRUCT
  66.         {
  67.  
  68.             public IntPtr hwnd;
  69.             [MarshalAs(UnmanagedType.U4)]
  70.             public FileOperationType wFunc;
  71.             public string pFrom;
  72.             public string pTo;
  73.             public FileOperationFlags fFlags;
  74.             [MarshalAs(UnmanagedType.Bool)]
  75.             public bool fAnyOperationsAborted;
  76.             public IntPtr hNameMappings;
  77.             public string lpszProgressTitle;
  78.         }
  79.  
  80.         [DllImport("shell32.dll", CharSet = CharSet.Auto)]
  81.         private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
  82.  
  83.         /// <summary>
  84.         /// Send file to recycle bin
  85.         /// </summary>
  86.         /// <param name="path">Location of directory or file to recycle</param>
  87.         /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
  88.         public static bool Send(string path, FileOperationFlags flags)
  89.         {
  90.             try
  91.             {
  92.                 var fs = new SHFILEOPSTRUCT
  93.                                         {
  94.                                             wFunc = FileOperationType.FO_DELETE,
  95.                                             pFrom = path + '\0' + '\0',
  96.                                             fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
  97.                                         };
  98.                 SHFileOperation(ref fs);
  99.                 return true;
  100.             }
  101.             catch (Exception)
  102.             {
  103.                 return false;
  104.             }
  105.         }
  106.  
  107.         /// <summary>
  108.         /// Send file to recycle bin.  Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING)
  109.         /// </summary>
  110.         /// <param name="path">Location of directory or file to recycle</param>
  111.         public static bool Send(string path)
  112.         {
  113.             return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Send file silently to recycle bin.  Surpress dialog, surpress errors, delete if too large.
  118.         /// </summary>
  119.         /// <param name="path">Location of directory or file to recycle</param>
  120.         public static bool MoveToRecycleBin(string path)
  121.         {
  122.             return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);
  123.  
  124.         }
  125.  
  126.         private static bool deleteFile(string path, FileOperationFlags flags)
  127.         {
  128.             try
  129.             {
  130.                 var fs = new SHFILEOPSTRUCT
  131.                                         {
  132.                                             wFunc = FileOperationType.FO_DELETE,
  133.                                             pFrom = path + '\0' + '\0',
  134.                                             fFlags = flags
  135.                                         };
  136.                 SHFileOperation(ref fs);
  137.                 return true;
  138.             }
  139.             catch (Exception)
  140.             {
  141.                 return false;
  142.             }
  143.         }
  144.  
  145.         public static bool DeleteCompletelySilent(string path)
  146.         {
  147.             return deleteFile(path,
  148.                               FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI |
  149.                               FileOperationFlags.FOF_SILENT);
  150.         }
  151.     }
  152. // Original source: http://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement