protected override bool RunDialog(IntPtr hWndOwner)
        {
            bool result = false;
              if (_rootFolderLocation == IntPtr.Zero) {
            PInvoke.Shell32.SHGetSpecialFolderLocation(hWndOwner,
            (int)this._rootFolder, ref _rootFolderLocation);
            if (_rootFolderLocation == IntPtr.Zero) {
              PInvoke.Shell32.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;
              IntPtr pszPath = IntPtr.Zero;
              try {
            PInvoke.BROWSEINFO browseInfo = new PInvoke.BROWSEINFO();
            hglobal = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize);
            pszPath = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize);
            this._callback = new PInvoke.BrowseFolderCallbackProc(this.FolderBrowserCallback);
            browseInfo.pidlRoot = _rootFolderLocation;
            browseInfo.Owner = hWndOwner;
            browseInfo.pszDisplayName = hglobal;
            browseInfo.Title = this._descriptionText;
            browseInfo.Flags = _uiFlags;
            browseInfo.callback = this._callback;
            browseInfo.lParam = IntPtr.Zero;
            browseInfo.iImage = 0;
            pidl = PInvoke.Shell32.SHBrowseForFolder(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) {
              PInvoke.Shell32.SHGetPathFromIDList(pidl, pszPath);
              this._selectedPathNeedsCheck = true;
              this._selectedPath = Marshal.PtrToStringAuto(pszPath);
              result = true;
            }
              } finally {
            PInvoke.IMalloc sHMalloc = GetSHMalloc();
            sHMalloc.Free(_rootFolderLocation);
            _rootFolderLocation = IntPtr.Zero;
            if (pidl != IntPtr.Zero) {
              sHMalloc.Free(pidl);
            }
            if (pszPath != IntPtr.Zero) {
              Marshal.FreeHGlobal(pszPath);
            }
            if (hglobal != IntPtr.Zero) {
              Marshal.FreeHGlobal(hglobal);
            }
            this._callback = null;
              }
              return result;
        }
Beispiel #2
0
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            bool result = false;

            if (_rootFolderLocation == IntPtr.Zero)
            {
                PInvoke.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)_rootFolder, ref _rootFolderLocation);
                if (_rootFolderLocation == IntPtr.Zero)
                {
                    PInvoke.Shell32.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 (_newStyle)
            {
                _uiFlags += BrowseFlags.BIF_NEWDIALOGSTYLE;
            }

            if (!_showNewFolderButton)
            {
                _uiFlags += BrowseFlags.BIF_NONEWFOLDERBUTTON;
            }

            if (_showEditBox)
            {
                _uiFlags += BrowseFlags.BIF_EDITBOX;
            }

            if (_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;
            IntPtr pszPath = IntPtr.Zero;

            try {
                PInvoke.BROWSEINFO browseInfo = new PInvoke.BROWSEINFO();
                hglobal                   = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize);
                pszPath                   = Marshal.AllocHGlobal(MAX_PATH * Marshal.SystemDefaultCharSize);
                _callback                 = new PInvoke.BrowseFolderCallbackProc(FolderBrowserCallback);
                browseInfo.pidlRoot       = _rootFolderLocation;
                browseInfo.Owner          = hWndOwner;
                browseInfo.pszDisplayName = hglobal;
                browseInfo.Title          = _descriptionText;
                browseInfo.Flags          = _uiFlags;
                browseInfo.callback       = _callback;
                browseInfo.lParam         = IntPtr.Zero;
                browseInfo.iImage         = 0;
                pidl = PInvoke.Shell32.SHBrowseForFolder(browseInfo);
                if (((_uiFlags & BrowseFlags.BIF_BROWSEFORPRINTER) == BrowseFlags.BIF_BROWSEFORPRINTER) || ((_uiFlags & BrowseFlags.BIF_BROWSEFORCOMPUTER) == BrowseFlags.BIF_BROWSEFORCOMPUTER))
                {
                    _selectedPath = Marshal.PtrToStringAuto(browseInfo.pszDisplayName);
                    result        = true;
                }
                else if (pidl != IntPtr.Zero)
                {
                    PInvoke.Shell32.SHGetPathFromIDList(pidl, pszPath);
                    _selectedPathNeedsCheck = true;
                    _selectedPath           = Marshal.PtrToStringAuto(pszPath);
                    result = true;
                }
            } finally {
                PInvoke.IMalloc sHMalloc = GetSHMalloc();
                sHMalloc.Free(_rootFolderLocation);
                _rootFolderLocation = IntPtr.Zero;
                if (pidl != IntPtr.Zero)
                {
                    sHMalloc.Free(pidl);
                }

                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }

                if (hglobal != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hglobal);
                }

                _callback = null;
            }
            return(result);
        }