protected override bool RunDialog(IntPtr hWndOwner) { bool result = false; if (_rootFolderLocation == IntPtr.Zero) { ShellApi.SHGetSpecialFolderLocation(hWndOwner, (ShellApi.CSIDL) this._rootFolder, ref _rootFolderLocation); if (_rootFolderLocation == IntPtr.Zero) { ShellApi.SHGetSpecialFolderLocation(hWndOwner, 0, ref _rootFolderLocation); if (_rootFolderLocation == IntPtr.Zero) { throw new InvalidOperationException("FolderBrowserDialogNoRootFolder"); } } } _hwndEdit = IntPtr.Zero; //_uiFlags = 0; if (_dontIncludeNetworkFoldersBelowDomainLevel) { _uiFlags += BrowseFlags.BIF_DONTGOBELOWDOMAIN; } if (this._newStyle) { _uiFlags += BrowseFlags.BIF_NEWDIALOGSTYLE; } if (!this._showNewFolderButton) { _uiFlags += BrowseFlags.BIF_NONEWFOLDERBUTTON; } if (this._showEditBox) { _uiFlags += BrowseFlags.BIF_EDITBOX; } if (this._showBothFilesAndFolders) { _uiFlags += BrowseFlags.BIF_BROWSEINCLUDEFILES; } if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != ApartmentState.STA)) { throw new ThreadStateException("DebuggingException: ThreadMustBeSTA"); } IntPtr pidl = IntPtr.Zero; IntPtr hglobal = IntPtr.Zero; try { ShellApi.BROWSEINFO browseInfo = new ShellApi.BROWSEINFO(); hglobal = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize); this._callback = new ShellApi.BrowseCallbackProc(this.FolderBrowserCallback); browseInfo.pidlRoot = _rootFolderLocation; browseInfo.hwndOwner = hWndOwner; browseInfo.pszDisplayName = hglobal; browseInfo.lpszTitle = this._descriptionText; browseInfo.ulFlags = _uiFlags; browseInfo.lpfn = this._callback; browseInfo.lParam = IntPtr.Zero; browseInfo.iImage = 0; pidl = ShellApi.SHBrowseForFolder(ref browseInfo); if (((_uiFlags & BrowseFlags.BIF_BROWSEFORPRINTER) == BrowseFlags.BIF_BROWSEFORPRINTER) || ((_uiFlags & BrowseFlags.BIF_BROWSEFORCOMPUTER) == BrowseFlags.BIF_BROWSEFORCOMPUTER)) { this._selectedPath = Marshal.PtrToStringAuto(browseInfo.pszDisplayName); result = true; } else { if (pidl != IntPtr.Zero) { StringBuilder pszPath = new StringBuilder(MAX_PATH); ShellApi.SHGetPathFromIDList(pidl, pszPath); this._selectedPathNeedsCheck = true; this._selectedPath = pszPath.ToString(); result = true; } } } finally { ShellApi.IMalloc sHMalloc = GetSHMalloc(); sHMalloc.Free(_rootFolderLocation); _rootFolderLocation = IntPtr.Zero; if (pidl != IntPtr.Zero) { sHMalloc.Free(pidl); } if (hglobal != IntPtr.Zero) { Marshal.FreeHGlobal(hglobal); } this._callback = null; } return(result); }
public void ShowDialog() { m_FullName = ""; m_DisplayName = ""; // Get shell's memory allocator, it is needed to free some memory later IMalloc pMalloc; pMalloc = ShellFunctions.GetMalloc(); IntPtr pidlRoot; if (RootType == RootTypeOptions.BySpecialFolder) { ShellApi.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, UserToken, 0, out pidlRoot); } else // m_RootType = RootTypeOptions.ByPath { uint iAttribute; ShellApi.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute); } ShellApi.BROWSEINFO bi = new ShellApi.BROWSEINFO(); bi.hwndOwner = hwndOwner; bi.pidlRoot = pidlRoot; bi.pszDisplayName = new String(' ', 256); bi.lpszTitle = Title; bi.ulFlags = (uint)DetailsFlags; bi.lParam = 0; bi.lpfn = new ShellApi.BrowseCallbackProc(myBrowseCallbackProc); // Show dialog IntPtr pidlSelected; pidlSelected = ShellApi.SHBrowseForFolder(ref bi); // Save the display name m_DisplayName = bi.pszDisplayName; IShellFolder isf = ShellFunctions.GetDesktopFolder(); ShellApi.STRRET ptrDisplayName; isf.GetDisplayNameOf(pidlSelected, (uint)ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellApi.SHGNO.SHGDN_FORPARSING, out ptrDisplayName); String sDisplay; ShellApi.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay); m_FullName = sDisplay; if (pidlRoot != IntPtr.Zero) pMalloc.Free(pidlRoot); if (pidlSelected != IntPtr.Zero) pMalloc.Free(pidlSelected); Marshal.ReleaseComObject(isf); Marshal.ReleaseComObject(pMalloc); }
public void ShowDialog() { m_FullName = ""; m_DisplayName = ""; // Get shell's memory allocator, it is needed to free some memory later IMalloc pMalloc; pMalloc = ShellFunctions.GetMalloc(); IntPtr pidlRoot; if (RootType == RootTypeOptions.BySpecialFolder) { ShellApi.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, UserToken, 0, out pidlRoot); } else // m_RootType = RootTypeOptions.ByPath { uint iAttribute; ShellApi.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute); } ShellApi.BROWSEINFO bi = new ShellApi.BROWSEINFO(); bi.hwndOwner = hwndOwner; bi.pidlRoot = pidlRoot; bi.pszDisplayName = new String(' ', 256); bi.lpszTitle = Title; bi.ulFlags = (uint)DetailsFlags; bi.lParam = 0; bi.lpfn = new ShellApi.BrowseCallbackProc(this.myBrowseCallbackProc); // Show dialog IntPtr pidlSelected; pidlSelected = ShellLib.ShellApi.SHBrowseForFolder(ref bi); // Save the display name m_DisplayName = bi.pszDisplayName.ToString(); IShellFolder isf = ShellFunctions.GetDesktopFolder(); ShellApi.STRRET ptrDisplayName; isf.GetDisplayNameOf(pidlSelected, (uint)ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellApi.SHGNO.SHGDN_FORPARSING, out ptrDisplayName); String sDisplay; ShellLib.ShellApi.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay); m_FullName = sDisplay; if (pidlRoot != IntPtr.Zero) { pMalloc.Free(pidlRoot); } if (pidlSelected != IntPtr.Zero) { pMalloc.Free(pidlSelected); } Marshal.ReleaseComObject(isf); Marshal.ReleaseComObject(pMalloc); }