using System; using System.Runtime.InteropServices; namespace Wankuma.Interop { /// /// SHFileOperationの処理クラス /// public class InteropSHFileOperation { /// /// FO_Func /// public enum FO_Func : uint { /// /// Move /// FO_MOVE = 0x0001, /// /// Copy /// FO_COPY = 0x0002, /// /// Delete /// FO_DELETE = 0x0003, /// /// Rename /// FO_RENAME = 0x0004, } /// /// SHFILEOPSTRUCT /// struct SHFILEOPSTRUCT { public IntPtr hwnd; public FO_Func wFunc; [MarshalAs(UnmanagedType.LPWStr)] public string pFrom; [MarshalAs(UnmanagedType.LPWStr)] public string pTo; public ushort fFlags; public bool fAnyOperationsAborted; public IntPtr hNameMappings; [MarshalAs(UnmanagedType.LPWStr)] public string lpszProgressTitle; } [DllImport("shell32.dll", CharSet = CharSet.Unicode)] static extern int SHFileOperation([In] ref SHFILEOPSTRUCT lpFileOp); private SHFILEOPSTRUCT _ShFile; /// /// FILEOP_FLAGS /// public FILEOP_FLAGS fFlags; /// /// HWND /// public IntPtr hwnd { set { this._ShFile.hwnd = value; } } /// /// 処理モード /// public FO_Func wFunc { set { this._ShFile.wFunc = value; } } /// /// 処理From /// public string pFrom { set { this._ShFile.pFrom = value + '\0' + '\0'; } } /// /// 処理To /// public string pTo { set { this._ShFile.pTo = value + '\0' + '\0'; } } /// /// fAnyOperationsAborted /// public bool fAnyOperationsAborted { set { this._ShFile.fAnyOperationsAborted = value; } } /// /// hNameMappings /// public IntPtr hNameMappings { set { this._ShFile.hNameMappings = value; } } /// /// lpszProgressTitle /// public string lpszProgressTitle { set { this._ShFile.lpszProgressTitle = value + '\0'; } } /// /// InteropSHFileOperation /// public InteropSHFileOperation() { this.fFlags = new FILEOP_FLAGS(); this._ShFile = new SHFILEOPSTRUCT(); this._ShFile.hwnd = IntPtr.Zero; this._ShFile.wFunc = FO_Func.FO_COPY; this._ShFile.pFrom = ""; this._ShFile.pTo = ""; this._ShFile.fAnyOperationsAborted = false; this._ShFile.hNameMappings = IntPtr.Zero; this._ShFile.lpszProgressTitle = ""; } /// /// 処理実行 /// /// 成功したらtrue public bool Execute() { this._ShFile.fFlags = this.fFlags.Flag; int ReturnValue = SHFileOperation(ref this._ShFile); if ( ReturnValue == 0 ) { return true; } else { return false; } } /// /// /// public class FILEOP_FLAGS { private enum FILEOP_FLAGS_ENUM : ushort { /// /// The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited. /// FOF_MULTIDESTFILES = 0x0001, /// /// Not used. /// FOF_CONFIRMMOUSE = 0x0002, /// /// Do not display a progress dialog box. /// FOF_SILENT = 0x0004, // don't create progress/report /// /// Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists. /// FOF_RENAMEONCOLLISION = 0x0008, /// /// Respond with "Yes to All" for any dialog box that is displayed. /// FOF_NOCONFIRMATION = 0x0010, // Don't prompt the user. /// /// If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object containing their old and new names to the hNameMappings member. /// FOF_WANTMAPPINGHANDLE = 0x0020, // Fill in SHFILEOPSTRUCT.hNameMappings /// /// Preserve undo information, if possible. /// Operations can be undone only from the same process that performed the original operation. /// If pFrom does not contain fully qualified path and file names, this flag is ignored. /// // Must be freed using SHFreeNameMappings FOF_ALLOWUNDO = 0x0040, /// /// Perform the operation on files only if a wildcard file name (*.*) is specified. /// FOF_FILESONLY = 0x0080, // on *.*, do only files /// /// Display a progress dialog box but do not show the file names. /// FOF_SIMPLEPROGRESS = 0x0100, // means don't show names of files /// /// Do not confirm the creation of a new directory if the operation requires one to be created. /// FOF_NOCONFIRMMKDIR = 0x0200, // don't confirm making any needed dirs /// /// Do not display a user interface if an error occurs. /// FOF_NOERRORUI = 0x0400, // don't put up error UI /// /// Version 4.71. Do not copy the security attributes of the file. /// FOF_NOCOPYSECURITYATTRIBS = 0x0800, // dont copy NT file Security Attributes /// /// Only operate in the local directory. Don't operate recursively into subdirectories. /// FOF_NORECURSION = 0x1000, // don't recurse into directories. /// /// Version 5.0. Do not move connected files as a group. Only move the specified files. /// FOF_NO_CONNECTED_ELEMENTS = 0x2000, // don't operate on connected elements. /// /// Version 5.0. Send a warning if a file is being destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION. /// FOF_WANTNUKEWARNING = 0x4000, // during delete operation, warn if nuking instead of recycling (partially overrides FOF_NOCONFIRMATION) /// /// Treat reparse points as objects, not containers. /// You must set _WIN32_WINNT to 5.01 or later to use this flag. /// See Shell and Common Controls Versions for further discussion of versioning. /// FOF_NORECURSEREPARSE = 0x8000, // treat reparse points as objects, not containers } /// /// The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited. /// public bool FOF_MULTIDESTFILES = false; /// /// Not used. /// public bool FOF_CONFIRMMOUSE = false; /// /// Do not display a progress dialog box. /// public bool FOF_SILENT = false; /// /// Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists. /// public bool FOF_RENAMEONCOLLISION = false; /// /// Respond with "Yes to All" for any dialog box that is displayed. /// public bool FOF_NOCONFIRMATION = false; /// /// If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object containing their old and new names to the hNameMappings member. /// public bool FOF_WANTMAPPINGHANDLE = false; /// /// Preserve undo information, if possible. /// Operations can be undone only from the same process that performed the original operation. /// If pFrom does not contain fully qualified path and file names, this flag is ignored. /// public bool FOF_ALLOWUNDO = false; /// /// Perform the operation on files only if a wildcard file name (*.*) is specified. /// public bool FOF_FILESONLY = false; /// /// Display a progress dialog box but do not show the file names. /// public bool FOF_SIMPLEPROGRESS = false; /// /// Do not confirm the creation of a new directory if the operation requires one to be created. /// public bool FOF_NOCONFIRMMKDIR = false; /// /// Do not display a user interface if an error occurs. /// public bool FOF_NOERRORUI = false; /// /// Version 4.71. Do not copy the security attributes of the file. /// public bool FOF_NOCOPYSECURITYATTRIBS = false; /// /// Only operate in the local directory. Don't operate recursively into subdirectories. /// public bool FOF_NORECURSION = false; /// /// Version 5.0. Do not move connected files as a group. Only move the specified files. /// public bool FOF_NO_CONNECTED_ELEMENTS = false; /// /// Version 5.0. Send a warning if a file is being destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION. /// public bool FOF_WANTNUKEWARNING = false; /// /// Treat reparse points as objects, not containers. /// You must set _WIN32_WINNT to 5.01 or later to use this flag. /// See Shell and Common Controls Versions for further discussion of versioning. /// public bool FOF_NORECURSEREPARSE = false; /// /// フラグ /// public ushort Flag { get { ushort ReturnValue = 0; if ( this.FOF_MULTIDESTFILES == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_MULTIDESTFILES; if ( this.FOF_CONFIRMMOUSE == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_CONFIRMMOUSE; if ( this.FOF_SILENT == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_SILENT; if ( this.FOF_RENAMEONCOLLISION == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_RENAMEONCOLLISION; if ( this.FOF_NOCONFIRMATION == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCONFIRMATION; if ( this.FOF_WANTMAPPINGHANDLE == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_WANTMAPPINGHANDLE; if ( this.FOF_ALLOWUNDO == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_ALLOWUNDO; if ( this.FOF_FILESONLY == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_FILESONLY; if ( this.FOF_SIMPLEPROGRESS == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_SIMPLEPROGRESS; if ( this.FOF_NOCONFIRMMKDIR == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCONFIRMMKDIR; if ( this.FOF_NOERRORUI == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOERRORUI; if ( this.FOF_NOCOPYSECURITYATTRIBS == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NOCOPYSECURITYATTRIBS; if ( this.FOF_NORECURSION == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NORECURSION; if ( this.FOF_NO_CONNECTED_ELEMENTS == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NO_CONNECTED_ELEMENTS; if ( this.FOF_WANTNUKEWARNING == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_WANTNUKEWARNING; if ( this.FOF_NORECURSEREPARSE == true ) ReturnValue |= (ushort)FILEOP_FLAGS_ENUM.FOF_NORECURSEREPARSE; return ReturnValue; } } } } }