/// <summary> /// Show the folder browser dialog. /// </summary> /// <param name="owner">Any object that implements IWin32Window that represents the top-level window that will own the modal dialog box.</param> /// <returns>DialogResult.OK if the user clicks OK in the dialog box; otherwise, DialogResult.Cancel.</returns> public DialogResult ShowDialog(IWin32Window owner) { string displayName = new string('\0', NativeMethods.MAX_PATH); NativeMethods.BrowseCallBackProc callback = new NativeMethods.BrowseCallBackProc(this.BrowseCallBackProc); NativeMethods.BROWSEINFO bi = new NativeMethods.BROWSEINFO(); IntPtr returnedPidl = IntPtr.Zero; try { bi.hwndOwner = owner.Handle; bi.pidlRoot = IntPtr.Zero; bi.pszDisplayName = displayName; bi.lpszTitle = this.description; bi.ulFlags = (int)NativeMethods.BrowseInfoFlags.BIF_EDITBOX | (int)NativeMethods.BrowseInfoFlags.BIF_NEWDIALOGSTYLE | (int)NativeMethods.BrowseInfoFlags.BIF_NONEWFOLDERBUTTON; bi.lpfn = callback; bi.lParam = IntPtr.Zero; bi.iImage = 0; returnedPidl = NativeMethods.SHBrowseForFolder(ref bi); if (returnedPidl != IntPtr.Zero) { StringBuilder tempSelectedPath = new StringBuilder(NativeMethods.MAX_PATH); if (NativeMethods.SHGetPathFromIDList(returnedPidl, tempSelectedPath)) { this.SelectedPath = tempSelectedPath.ToString(); return(DialogResult.OK); } } return(DialogResult.Cancel); } finally { if (returnedPidl != IntPtr.Zero) { Marshal.FreeCoTaskMem(returnedPidl); } } }
/// <summary> /// Show the folder browser dialog. /// </summary> /// <param name="owner">Any object that implements IWin32Window that represents the top-level window that will own the modal dialog box.</param> /// <returns>DialogResult.OK if the user clicks OK in the dialog box; otherwise, DialogResult.Cancel.</returns> public DialogResult ShowDialog(IWin32Window owner) { string displayName = new string('\0', NativeMethods.MAX_PATH); NativeMethods.BrowseCallBackProc callback = new NativeMethods.BrowseCallBackProc(this.BrowseCallBackProc); NativeMethods.BROWSEINFO bi = new NativeMethods.BROWSEINFO(); IntPtr returnedPidl = IntPtr.Zero; try { bi.hwndOwner = owner.Handle; bi.pidlRoot = IntPtr.Zero; bi.pszDisplayName = displayName; bi.lpszTitle = this.description; bi.ulFlags = (int)NativeMethods.BrowseInfoFlags.BIF_EDITBOX | (int)NativeMethods.BrowseInfoFlags.BIF_NEWDIALOGSTYLE | (int)NativeMethods.BrowseInfoFlags.BIF_NONEWFOLDERBUTTON; bi.lpfn = callback; bi.lParam = IntPtr.Zero; bi.iImage = 0; returnedPidl = NativeMethods.SHBrowseForFolder(ref bi); if (returnedPidl != IntPtr.Zero) { StringBuilder tempSelectedPath = new StringBuilder(NativeMethods.MAX_PATH); if (NativeMethods.SHGetPathFromIDList(returnedPidl, tempSelectedPath)) { this.SelectedPath = tempSelectedPath.ToString(); return DialogResult.OK; } } return DialogResult.Cancel; } finally { if (returnedPidl != IntPtr.Zero) { Marshal.FreeCoTaskMem(returnedPidl); } } }