From 102fac39d121b9d9809f8d50b935fafaeade76ab Mon Sep 17 00:00:00 2001 From: dannoe Date: Tue, 28 Jun 2016 22:10:53 +0200 Subject: [PATCH] Files and folders are now opened in the default filemanager. --- ShareX.HelpersLib/Helpers/Helpers.cs | 4 ++-- ShareX.HelpersLib/Native/NativeMethods.cs | 10 ++++++++++ ShareX.HelpersLib/Native/NativeMethods_Helpers.cs | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index 797ed551c..5bf9ecc11 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -417,7 +417,7 @@ public static bool OpenFolder(string folderPath) { try { - Process.Start("explorer.exe", folderPath); + Process.Start(folderPath); return true; } catch (Exception e) @@ -439,7 +439,7 @@ public static bool OpenFolderWithFile(string filePath) { try { - Process.Start("explorer.exe", $"/select,\"{filePath}\""); + NativeMethods.OpenFolderAndSelectFile(filePath); return true; } catch (Exception e) diff --git a/ShareX.HelpersLib/Native/NativeMethods.cs b/ShareX.HelpersLib/Native/NativeMethods.cs index 91760f4ca..0f1c0707a 100644 --- a/ShareX.HelpersLib/Native/NativeMethods.cs +++ b/ShareX.HelpersLib/Native/NativeMethods.cs @@ -363,6 +363,16 @@ public static partial class NativeMethods [DllImport("shell32.dll")] public static extern IntPtr SHAppBarMessage(uint dwMessage, [In] ref APPBARDATA pData); + // http://stackoverflow.com/questions/14600987/code-to-open-windows-explorer-or-focus-if-exists-with-file-selected + [DllImport("shell32.dll")] + public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags); + + [DllImport("shell32.dll", CharSet = CharSet.Unicode)] + public static extern IntPtr ILCreateFromPathW(string pszPath); + + [DllImport("shell32.dll")] + public static extern void ILFree(IntPtr pidl); + #endregion shell32.dll #region dwmapi.dll diff --git a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs index b21db35ab..af09b5148 100644 --- a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs +++ b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs @@ -476,5 +476,16 @@ public static bool FlashWindowEx(Form frm, uint flashCount = uint.MaxValue) return FlashWindowEx(ref fInfo); } + + // http://stackoverflow.com/questions/14600987/code-to-open-windows-explorer-or-focus-if-exists-with-file-selected + public static void OpenFolderAndSelectFile(string filePath) + { + if (filePath == null) + throw new ArgumentNullException(nameof(filePath)); + + IntPtr pidl = ILCreateFromPathW(filePath); + SHOpenFolderAndSelectItems(pidl, 0, IntPtr.Zero, 0); + ILFree(pidl); + } } } \ No newline at end of file