protected override Boolean RunDialog(IntPtr hwndOwner) { IntPtr pidlRoot = IntPtr.Zero; Boolean result = false; UnsafeNativeMethods.Shell32.SHGetSpecialFolderLocation(hwndOwner, (Int32)_rootFolder, ref pidlRoot); if (pidlRoot == IntPtr.Zero) { UnsafeNativeMethods.Shell32.SHGetSpecialFolderLocation(hwndOwner, 0, ref pidlRoot); if (pidlRoot == IntPtr.Zero) { throw new InvalidOperationException("Unable to retrieve the root folder."); } } Int32 options = NEW_DIALOG_STYLE_OPTION; if (!ShowNewFolderButton) { options += HIDE_NEW_FOLDER_BUTTON_OPTION; } IntPtr pidlRet = IntPtr.Zero; IntPtr pszDisplayName = IntPtr.Zero; IntPtr pszSelectedPath = IntPtr.Zero; UnsafeNativeMethods.BrowseCallbackProc callback; try { var browseInfo = new UnsafeNativeMethods.BrowseInfo(); pszDisplayName = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize); pszSelectedPath = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize); callback = new UnsafeNativeMethods.BrowseCallbackProc(BrowseCallbackHandler); browseInfo.pidlRoot = pidlRoot; browseInfo.hwndOwner = hwndOwner; browseInfo.pszDisplayName = pszDisplayName; browseInfo.lpszTitle = _descriptionText; browseInfo.ulFlags = options; browseInfo.lpfn = callback; browseInfo.lParam = IntPtr.Zero; browseInfo.iImage = 0; pidlRet = UnsafeNativeMethods.Shell32.SHBrowseForFolder(browseInfo); if (pidlRet != IntPtr.Zero) { UnsafeNativeMethods.Shell32.SHGetPathFromIDList(pidlRet, pszSelectedPath); _selectedPathNeedsCheck = true; _selectedPath = Marshal.PtrToStringAuto(pszSelectedPath); result = true; } } finally { UnsafeNativeMethods.Ole32.CoTaskMemFree(pidlRoot); if (pidlRet != IntPtr.Zero) { UnsafeNativeMethods.Ole32.CoTaskMemFree(pidlRet); } if (pszSelectedPath != IntPtr.Zero) { Marshal.FreeHGlobal(pszSelectedPath); } if (pszDisplayName != IntPtr.Zero) { Marshal.FreeHGlobal(pszDisplayName); } callback = null; } return(result); }
public static extern IntPtr SHBrowseForFolder([In] BrowseInfo lpbi);