rawfilewizard

git clone https://git.clttr.info/rawfilewizard.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

FileOperationAPIWrapper.cs (5553B)


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