From ccabf63fc4cf7c61bfbf8aa8762488944bb7ac33 Mon Sep 17 00:00:00 2001 From: morkt Date: Sat, 30 Apr 2022 11:29:22 +0400 Subject: [PATCH] (GUI): fixed deleting of files on 64-bit systems. --- GUI/MainWindow.xaml.cs | 3 +- GUI/Shell.cs | 93 ++++++++++++++++++++++++++++++------------ 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/GUI/MainWindow.xaml.cs b/GUI/MainWindow.xaml.cs index 9f83bf1d..e64326c0 100644 --- a/GUI/MainWindow.xaml.cs +++ b/GUI/MainWindow.xaml.cs @@ -1133,7 +1133,8 @@ namespace GARbro.GUI try { var file_list = items.Select (entry => Path.Combine (CurrentPath, entry.Name)); - GARbro.Shell.File.Delete (file_list); + if (!GARbro.Shell.File.Delete (file_list, new WindowInteropHelper(this).Handle)) + throw new ApplicationException ("Delete operation failed."); count = file_list.Count(); } finally diff --git a/GUI/Shell.cs b/GUI/Shell.cs index 3b6a233a..5a7e7439 100644 --- a/GUI/Shell.cs +++ b/GUI/Shell.cs @@ -119,42 +119,86 @@ namespace GARbro.Shell /// /// SHFILEOPSTRUCT for SHFileOperation from COM /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)] - private struct SHFILEOPSTRUCT + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + private struct SHFILEOPSTRUCT32 { - public IntPtr hwnd; [MarshalAs(UnmanagedType.U4)] public FileOperationType wFunc; + [MarshalAs(UnmanagedType.LPTStr)] public string pFrom; + [MarshalAs(UnmanagedType.LPTStr)] public string pTo; public FileOperationFlags fFlags; [MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted; public IntPtr hNameMappings; + [MarshalAs(UnmanagedType.LPTStr)] public string lpszProgressTitle; } - [DllImport("shell32.dll", CharSet = CharSet.Auto)] - private static extern int SHFileOperation (ref SHFILEOPSTRUCT FileOp); + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct SHFILEOPSTRUCT64 + { + public IntPtr hwnd; + [MarshalAs(UnmanagedType.U4)] + public FileOperationType wFunc; + [MarshalAs(UnmanagedType.LPTStr)] + public string pFrom; + [MarshalAs(UnmanagedType.LPTStr)] + public string pTo; + public FileOperationFlags fFlags; + public bool fAnyOperationsAborted; + public IntPtr hNameMappings; + [MarshalAs(UnmanagedType.LPTStr)] + public string lpszProgressTitle; + } + + [DllImport("shell32.dll", EntryPoint = "SHFileOperationW", CharSet = CharSet.Unicode)] + private static extern int SHFileOperation32 (ref SHFILEOPSTRUCT32 FileOp); + + [DllImport("shell32.dll", EntryPoint = "SHFileOperationW", CharSet = CharSet.Unicode)] + private static extern int SHFileOperation64 (ref SHFILEOPSTRUCT64 lpFileOp); + + private static int SHFileOperation (FileOperationType func, string path, FileOperationFlags flags, IntPtr parent) + { + if (Marshal.SizeOf(typeof(IntPtr)) == 4) + { + var fs = new SHFILEOPSTRUCT32 + { + hwnd = parent, + wFunc = func, + pFrom = path, + fFlags = flags + }; + return SHFileOperation32 (ref fs); + } + else + { + var fs = new SHFILEOPSTRUCT64 + { + hwnd = parent, + wFunc = func, + pFrom = path, + fFlags = flags + }; + return SHFileOperation64 (ref fs); + } + } + /// /// Send file to recycle bin /// /// Location of directory or file to recycle /// FileOperationFlags to add in addition to FOF_ALLOWUNDO - public static bool Delete (string path, FileOperationFlags flags) + public static bool Delete (string path, FileOperationFlags flags, IntPtr parent = default(IntPtr)) { - var fs = new SHFILEOPSTRUCT - { - wFunc = FileOperationType.FO_DELETE, - pFrom = path + '\0' + '\0', - fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags - }; - return 0 == SHFileOperation (ref fs); + return 0 == SHFileOperation (FileOperationType.FO_DELETE, path+'\0'+'\0', + FileOperationFlags.FOF_ALLOWUNDO | flags, parent); } - public static bool Delete (IEnumerable file_list, FileOperationFlags flags) + public static bool Delete (IEnumerable file_list, FileOperationFlags flags, IntPtr parent = default(IntPtr)) { var files = new StringBuilder(); foreach (var file in file_list) @@ -165,36 +209,31 @@ namespace GARbro.Shell if (0 == files.Length) return false; files.Append ('\0'); - var fs = new SHFILEOPSTRUCT - { - wFunc = FileOperationType.FO_DELETE, - pFrom = files.ToString(), - fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags - }; - return 0 == SHFileOperation (ref fs); + return 0 == SHFileOperation (FileOperationType.FO_DELETE, files.ToString(), + FileOperationFlags.FOF_ALLOWUNDO | flags, parent); } - public static bool Delete (IEnumerable file_list) + public static bool Delete (IEnumerable file_list, IntPtr parent = default(IntPtr)) { - return Delete (file_list, FileOperationFlags.FOF_WANTNUKEWARNING); + return Delete (file_list, FileOperationFlags.FOF_WANTNUKEWARNING, parent); } /// /// Send file to recycle bin. Display dialog, display warning if files are too big to fit (FOF_WANTNUKEWARNING) /// /// Location of directory or file to recycle - public static bool Delete (string path) + public static bool Delete (string path, IntPtr parent = default(IntPtr)) { - return Delete (path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING); + return Delete (path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING, parent); } /// /// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large. /// /// Location of directory or file to recycle - public static bool MoveToRecycleBin (string path) + public static bool MoveToRecycleBin (string path, IntPtr parent = default(IntPtr)) { - return Delete (path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT); + return Delete (path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT, parent); }