public static String getFileSystemPath(int csidl) { IntPtr pIDL = new IntPtr(); int hRes = ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, (ShellApi.CSIDL)csidl, ref pIDL); if (hRes != 0) { //throw Marshal.ThrowExceptionForHR(hRes); // TODO exception for hRes return(null); } StringBuilder builder = new StringBuilder(1024); if (ShellApi.SHGetPathFromIDList(pIDL, builder)) { return(builder.ToString()); } else { return(null); } }
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); }
private int FolderBrowserCallback(IntPtr hwnd, int msg, IntPtr lParam, IntPtr lpData) { switch (msg) { case BrowseForFolderMessages.BFFM_INITIALIZED: if (this._selectedPath.Length != 0) { PInvoke.User32.SendMessage(new HandleRef(null, hwnd), BrowseForFolderMessages.BFFM_SETSELECTIONW, 1, this._selectedPath); if (this._showEditBox && this._showFullPathInEditBox) { // get handle to the Edit box inside the Folder Browser Dialog _hwndEdit = PInvoke.User32.FindWindowEx(new HandleRef(null, hwnd), IntPtr.Zero, "Edit", null); PInvoke.User32.SetWindowText(_hwndEdit, this._selectedPath); } } break; case BrowseForFolderMessages.BFFM_IUNKNOWN: IntPtr iunknown = lParam; IntPtr iFolderFilterSite; if (iunknown == IntPtr.Zero) { break; } System.Runtime.InteropServices.Marshal.QueryInterface( iunknown, ref IID_IFolderFilterSite, out iFolderFilterSite); Object obj = System.Runtime.InteropServices.Marshal.GetTypedObjectForIUnknown( iFolderFilterSite, System.Type.GetType("ShellLib.IFolderFilterSite")); ShellLib.IFolderFilterSite folderFilterSite = (ShellLib.IFolderFilterSite)obj; FilterByExtension filter = new FilterByExtension(); filter.ValidExtensions = _validExtensions; filter.DontExpandZip = _dontExpandZip; folderFilterSite.SetFilter(filter); break; case BrowseForFolderMessages.BFFM_SELCHANGED: IntPtr pidl = lParam; if (pidl != IntPtr.Zero) { if (((_uiFlags & BrowseFlags.BIF_BROWSEFORPRINTER) == BrowseFlags.BIF_BROWSEFORPRINTER) || ((_uiFlags & BrowseFlags.BIF_BROWSEFORCOMPUTER) == BrowseFlags.BIF_BROWSEFORCOMPUTER)) { // we're browsing for a printer or computer, enable the OK button unconditionally. PInvoke.User32.SendMessage(new HandleRef(null, hwnd), BrowseForFolderMessages.BFFM_ENABLEOK, 0, 1); } else { //IntPtr pszPath = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize); StringBuilder pszPath = new StringBuilder(MAX_PATH); bool haveValidPath = ShellApi.SHGetPathFromIDList(pidl, pszPath); String displayedPath = pszPath.ToString(); // whether to enable the OK button or not. (if file is valid) PInvoke.User32.SendMessage(new HandleRef(null, hwnd), BrowseForFolderMessages.BFFM_ENABLEOK, 0, haveValidPath ? 1 : 0); // Maybe set the Edit Box text to the Full Folder path if (haveValidPath && !String.IsNullOrEmpty(displayedPath)) { if (_showEditBox && _showFullPathInEditBox) { if (_hwndEdit != IntPtr.Zero) { PInvoke.User32.SetWindowText(_hwndEdit, displayedPath); } } if ((_uiFlags & BrowseFlags.BIF_STATUSTEXT) == BrowseFlags.BIF_STATUSTEXT) { PInvoke.User32.SendMessage(new HandleRef(null, hwnd), BrowseForFolderMessages.BFFM_SETSTATUSTEXT, 0, displayedPath); } } } } break; } return(0); }