using System; using System.Threading.Tasks; using System.Net; public static class DisplayFusionFunction { public static void Run(IntPtr windowHandle) { ExplorerWindows.InitExplorerHandler(); BFS.Dialog.ShowMessageInfo(ExplorerWindows.GetPath(windowHandle)); } } // The following code is adapted from this solution: // https://stackoverflow.com/questions/36886355/retrieve-the-full-path-of-an-explorer-window-through-a-handle-in-c-sharp public static class ExplorerWindows { static Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000"); static Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true); static object shellApplication = null; static object windows = null; static Type windowsType = null; static object count = null; public static void InitExplorerHandler() { shellApplication = Activator.CreateInstance(shellApplicationType); windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { }); windowsType = windows.GetType(); count = windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null); } public static string GetPath(IntPtr windowHandle) { string currDirectory = ""; Parallel.For(0, (int)count, i => { try { object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i }); Type itemType = item.GetType(); string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null); if (itemName == "Windows Explorer" || itemName == "File Explorer" || itemName == "Explorer") { string itemHandle = itemType.InvokeMember("HWND", System.Reflection.BindingFlags.GetProperty, null, item, null).ToString(); if (itemHandle == windowHandle.ToString()) { currDirectory = (string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null); } } } catch (Exception e) { } }); return WebUtility.HtmlDecode(currDirectory); } }