/// <summary> /// Opens the requested directory in the shell's file/folder browser. /// </summary> /// <param name="parent">The window that is currently in the foreground.</param> /// <param name="folderPath">The folder to open.</param> /// <remarks> /// This UI is presented modelessly, in another process, and in the foreground. /// Error handling and messaging (error dialogs) will be handled by the shell, /// and these errors will not be communicated to the caller of this method. /// </remarks> public static void BrowseFolder(IWin32Window parent, string folderPath) { NativeStructs.SHELLEXECUTEINFO sei = new NativeStructs.SHELLEXECUTEINFO(); sei.cbSize = (uint)Marshal.SizeOf(typeof(NativeStructs.SHELLEXECUTEINFO)); sei.fMask = NativeConstants.SEE_MASK_NO_CONSOLE; sei.lpVerb = "open"; sei.lpFile = folderPath; sei.nShow = NativeConstants.SW_SHOWNORMAL; sei.hwnd = parent.Handle; bool bResult = NativeMethods.ShellExecuteExW(ref sei); if (bResult) { if (sei.hProcess != IntPtr.Zero) { SafeNativeMethods.CloseHandle(sei.hProcess); sei.hProcess = IntPtr.Zero; } } else { NativeMethods.ThrowOnWin32Error("ShellExecuteW returned FALSE"); } GC.KeepAlive(parent); }
private static int ExecShellExecuteEx(IWin32Window parent, string exePath, string args, string verb, out IntPtr hProcess) { string dir; try { dir = Path.GetDirectoryName(exePath); } catch (Exception) { dir = null; } NativeStructs.SHELLEXECUTEINFO sei = new NativeStructs.SHELLEXECUTEINFO(); sei.cbSize = (uint)Marshal.SizeOf(typeof(NativeStructs.SHELLEXECUTEINFO)); sei.fMask = NativeConstants.SEE_MASK_NOCLOSEPROCESS | NativeConstants.SEE_MASK_NO_CONSOLE | NativeConstants.SEE_MASK_FLAG_DDEWAIT; sei.lpVerb = verb; sei.lpDirectory = dir; sei.lpFile = exePath; sei.lpParameters = args; sei.nShow = NativeConstants.SW_SHOWNORMAL; if (parent != null) { sei.hwnd = parent.Handle; } bool bResult = NativeMethods.ShellExecuteExW(ref sei); hProcess = sei.hProcess; sei.hProcess = IntPtr.Zero; int nResult = NativeConstants.ERROR_SUCCESS; if (!bResult) { nResult = Marshal.GetLastWin32Error(); } return(nResult); }
internal static extern bool ShellExecuteExW(ref NativeStructs.SHELLEXECUTEINFO lpExecInfo);