private bool RunDialogDownlevel(IntPtr owner) { IntPtr rootItemIdList = IntPtr.Zero; IntPtr resultItemIdList = IntPtr.Zero; if (NativeMethods.SHGetSpecialFolderLocation(owner, RootFolder, ref rootItemIdList) != 0) { if (NativeMethods.SHGetSpecialFolderLocation(owner, 0, ref rootItemIdList) != 0) { throw new InvalidOperationException("Unable to retrieve the root folder."); } } try { NativeMethods.BROWSEINFO info = new NativeMethods.BROWSEINFO { hwndOwner = owner, lpfn = new NativeMethods.BrowseCallbackProc(BrowseCallbackProc), lpszTitle = Description, pidlRoot = rootItemIdList, pszDisplayName = new string('\0', 260), ulFlags = NativeMethods.BrowseInfoFlags.NewDialogStyle | NativeMethods.BrowseInfoFlags.ReturnOnlyFsDirs }; if (!ShowNewFolderButton) { info.ulFlags |= NativeMethods.BrowseInfoFlags.NoNewFolderButton; } resultItemIdList = NativeMethods.SHBrowseForFolder(ref info); if (resultItemIdList != IntPtr.Zero) { StringBuilder path = new StringBuilder(260); NativeMethods.SHGetPathFromIDList(resultItemIdList, path); SelectedPath = path.ToString(); return(true); } else { return(false); } } finally { if (rootItemIdList != null) { IMalloc malloc = NativeMethods.SHGetMalloc(); malloc.Free(rootItemIdList); Marshal.ReleaseComObject(malloc); } if (resultItemIdList != null) { Marshal.FreeCoTaskMem(resultItemIdList); } } }
private bool RunDialogDownlevel(IntPtr owner) { IntPtr rootItemIdList = IntPtr.Zero; IntPtr resultItemIdList = IntPtr.Zero; if( NativeMethods.SHGetSpecialFolderLocation(owner, RootFolder, ref rootItemIdList) != 0 ) { if( NativeMethods.SHGetSpecialFolderLocation(owner, 0, ref rootItemIdList) != 0 ) { throw new InvalidOperationException(Properties.Resources.FolderBrowserDialogNoRootFolder); } } try { NativeMethods.BROWSEINFO info = new NativeMethods.BROWSEINFO(); info.hwndOwner = owner; info.lpfn = new NativeMethods.BrowseCallbackProc(BrowseCallbackProc); info.lpszTitle = Description; info.pidlRoot = rootItemIdList; info.pszDisplayName = new string('\0', 260); info.ulFlags = NativeMethods.BrowseInfoFlags.NewDialogStyle | NativeMethods.BrowseInfoFlags.ReturnOnlyFsDirs; if( !ShowNewFolderButton ) info.ulFlags |= NativeMethods.BrowseInfoFlags.NoNewFolderButton; resultItemIdList = NativeMethods.SHBrowseForFolder(ref info); if( resultItemIdList != IntPtr.Zero ) { StringBuilder path = new StringBuilder(260); NativeMethods.SHGetPathFromIDList(resultItemIdList, path); SelectedPath = path.ToString(); return true; } else return false; } finally { if( rootItemIdList != null ) { IMalloc malloc = NativeMethods.SHGetMalloc(); malloc.Free(rootItemIdList); Marshal.ReleaseComObject(malloc); } if( resultItemIdList != null ) { Marshal.FreeCoTaskMem(resultItemIdList); } } }