public static extern IntPtr SHBrowseForFolder(ref BrowseInfo lbpi);
/// <summary> /// Displays the folder browser dialog. /// </summary> /// <param name="hwndOwner">Handle to the window that owns the dialog box.</param> /// <returns> /// If the user clicks the OK button of the dialog that is displayed, true is returned; otherwise, false. /// </returns> protected override bool RunDialog(IntPtr hwndOwner) { var result = false; IntPtr pidlRoot = IntPtr.Zero, pszPath = IntPtr.Zero, pidlSelected = IntPtr.Zero; SelectedPath = string.Empty; try { if (UseSpecialFolderRoot) Shell32.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, IntPtr.Zero, 0, out pidlRoot); else // RootType == Path { uint iAttribute; Shell32.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute); } var browseInfo = new BrowseInfo { HwndOwner = hwndOwner, Root = pidlRoot, DisplayName = new string(' ', 256), Title = Title, Flags = _dialogOptions, LParam = 0, Callback = HookProc }; // Show dialog pidlSelected = Shell32.SHBrowseForFolder(ref browseInfo); if (pidlSelected != IntPtr.Zero) { result = true; pszPath = Marshal.AllocHGlobal(260 * Marshal.SystemDefaultCharSize); Shell32.SHGetPathFromIDList(pidlSelected, pszPath); SelectedPath = Marshal.PtrToStringAuto(pszPath); } } finally // release all unmanaged resources { var malloc = GetSHMalloc(); if (pidlRoot != IntPtr.Zero) malloc.Free(pidlRoot); if (pidlSelected != IntPtr.Zero) malloc.Free(pidlSelected); if (pszPath != IntPtr.Zero) Marshal.FreeHGlobal(pszPath); Marshal.ReleaseComObject(malloc); } return result; }