/// <summary> /// Shows the folder browser dialog box with the specified owner window. /// </summary> public DialogResult ShowDialog(IWin32Window owner) { IntPtr pidlRoot = IntPtr.Zero; // Get/find an owner HWND for this dialog. IntPtr hWndOwner; if (owner != null) { hWndOwner = owner.Handle; } else { hWndOwner = WinAPI.GetActiveWindow(); } // Get the IDL for the specific startLocation. WinAPI.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)startLocation, out pidlRoot); if (pidlRoot == IntPtr.Zero) { return DialogResult.Cancel; } int mergedOptions = (int)publicOptions | (int)privateOptions; if ((mergedOptions & (int)WinAPI.Shell32.BffStyles.NewDialogStyle) != 0) { if (System.Threading.ApartmentState.MTA == Application.OleRequired()) mergedOptions = mergedOptions & (~(int)WinAPI.Shell32.BffStyles.NewDialogStyle); } IntPtr pidlRet = IntPtr.Zero; try { // Construct a BROWSEINFO. WinAPI.Shell32.BROWSEINFO bi = new WinAPI.Shell32.BROWSEINFO(); IntPtr buffer = Marshal.AllocHGlobal(MAX_PATH); bi.pidlRoot = pidlRoot; bi.hwndOwner = hWndOwner; bi.pszDisplayName = buffer; bi.lpszTitle = descriptionText; bi.ulFlags = mergedOptions; // The rest of the fields are initialized to zero by the constructor. // bi.lpfn = null; bi.lParam = IntPtr.Zero; bi.iImage = 0; // Show the dialog. pidlRet = WinAPI.Shell32.SHBrowseForFolder(ref bi); // Free the buffer you've allocated on the global heap. Marshal.FreeHGlobal(buffer); if (pidlRet == IntPtr.Zero) { // User clicked Cancel. return DialogResult.Cancel; } // Then retrieve the path from the IDList. StringBuilder sb = new StringBuilder(MAX_PATH); if (0 == WinAPI.Shell32.SHGetPathFromIDList(pidlRet, sb)) { return DialogResult.Cancel; } // Convert to a string. directoryPath = sb.ToString(); } finally { WinAPI.IMalloc malloc = GetSHMalloc(); malloc.Free(pidlRoot); if (pidlRet != IntPtr.Zero) { malloc.Free(pidlRet); } } return DialogResult.OK; }
/// <summary> /// Shows the folder browser dialog box with the specified owner window. /// </summary> public DialogResult ShowDialog(IWin32Window owner) { IntPtr pidlRoot = IntPtr.Zero; // Get/find an owner HWND for this dialog. IntPtr hWndOwner; if (owner != null) { hWndOwner = owner.Handle; } else { hWndOwner = WinAPI.GetActiveWindow(); } // Get the IDL for the specific startLocation. WinAPI.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)startLocation, out pidlRoot); if (pidlRoot == IntPtr.Zero) { return(DialogResult.Cancel); } int mergedOptions = (int)publicOptions | (int)privateOptions; if ((mergedOptions & (int)WinAPI.Shell32.BffStyles.NewDialogStyle) != 0) { if (System.Threading.ApartmentState.MTA == Application.OleRequired()) { mergedOptions = mergedOptions & (~(int)WinAPI.Shell32.BffStyles.NewDialogStyle); } } IntPtr pidlRet = IntPtr.Zero; try { // Construct a BROWSEINFO. WinAPI.Shell32.BROWSEINFO bi = new WinAPI.Shell32.BROWSEINFO(); IntPtr buffer = Marshal.AllocHGlobal(MAX_PATH); bi.pidlRoot = pidlRoot; bi.hwndOwner = hWndOwner; bi.pszDisplayName = buffer; bi.lpszTitle = descriptionText; bi.ulFlags = mergedOptions; // The rest of the fields are initialized to zero by the constructor. // bi.lpfn = null; bi.lParam = IntPtr.Zero; bi.iImage = 0; // Show the dialog. pidlRet = WinAPI.Shell32.SHBrowseForFolder(ref bi); // Free the buffer you've allocated on the global heap. Marshal.FreeHGlobal(buffer); if (pidlRet == IntPtr.Zero) { // User clicked Cancel. return(DialogResult.Cancel); } // Then retrieve the path from the IDList. StringBuilder sb = new StringBuilder(MAX_PATH); if (0 == WinAPI.Shell32.SHGetPathFromIDList(pidlRet, sb)) { return(DialogResult.Cancel); } // Convert to a string. directoryPath = sb.ToString(); } finally { WinAPI.IMalloc malloc = GetSHMalloc(); malloc.Free(pidlRoot); if (pidlRet != IntPtr.Zero) { malloc.Free(pidlRet); } } return(DialogResult.OK); }