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;
        }
        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);
        }
 public static extern IntPtr SHBrowseForFolder([In] PInvoke.BROWSEINFO lpbi);
Example #4
0
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            bool flag = false;

            if (this._rootFolderLocation == IntPtr.Zero)
            {
                PInvoke.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)this._rootFolder,
                                                           ref this._rootFolderLocation);
                if (this._rootFolderLocation == IntPtr.Zero)
                {
                    PInvoke.Shell32.SHGetSpecialFolderLocation(hWndOwner, 0, ref this._rootFolderLocation);
                    if (this._rootFolderLocation == IntPtr.Zero)
                    {
                        throw new InvalidOperationException("FolderBrowserDialogNoRootFolder");
                    }
                }
            }
            this._hwndEdit = IntPtr.Zero;
            if (this._dontIncludeNetworkFoldersBelowDomainLevel)
            {
                this._uiFlags += 2;
            }
            if (this._newStyle)
            {
                this._uiFlags += 0x40;
            }
            if (!this._showNewFolderButton)
            {
                this._uiFlags += 0x200;
            }
            if (this._showEditBox)
            {
                this._uiFlags += 0x10;
            }
            if (this._showBothFilesAndFolders)
            {
                this._uiFlags += 0x4000;
            }
            if (Control.CheckForIllegalCrossThreadCalls && (Application.OleRequired() != ApartmentState.STA))
            {
                throw new ThreadStateException("DebuggingException: ThreadMustBeSTA");
            }
            IntPtr zero    = IntPtr.Zero;
            IntPtr hglobal = IntPtr.Zero;
            IntPtr pszPath = IntPtr.Zero;

            try
            {
                PInvoke.BROWSEINFO lpbi = new PInvoke.BROWSEINFO();
                hglobal             = Marshal.AllocHGlobal((int)(MAX_PATH * Marshal.SystemDefaultCharSize));
                pszPath             = Marshal.AllocHGlobal((int)(MAX_PATH * Marshal.SystemDefaultCharSize));
                this._callback      = new PInvoke.BrowseFolderCallbackProc(this.FolderBrowserCallback);
                lpbi.pidlRoot       = this._rootFolderLocation;
                lpbi.Owner          = hWndOwner;
                lpbi.pszDisplayName = hglobal;
                lpbi.Title          = this._descriptionText;
                lpbi.Flags          = this._uiFlags;
                lpbi.callback       = this._callback;
                lpbi.lParam         = IntPtr.Zero;
                lpbi.iImage         = 0;
                zero = PInvoke.Shell32.SHBrowseForFolder(lpbi);
                if (((this._uiFlags & 0x2000) == 0x2000) || ((this._uiFlags & 0x1000) == 0x1000))
                {
                    this._selectedPath = Marshal.PtrToStringAuto(lpbi.pszDisplayName);
                    return(true);
                }
                if (zero != IntPtr.Zero)
                {
                    PInvoke.Shell32.SHGetPathFromIDList(zero, pszPath);
                    this._selectedPathNeedsCheck = true;
                    this._selectedPath           = Marshal.PtrToStringAuto(pszPath);
                    flag = true;
                }
            }
            finally
            {
                PInvoke.IMalloc sHMalloc = GetSHMalloc();
                sHMalloc.Free(this._rootFolderLocation);
                this._rootFolderLocation = IntPtr.Zero;
                if (zero != IntPtr.Zero)
                {
                    sHMalloc.Free(zero);
                }
                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }
                if (hglobal != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hglobal);
                }
                this._callback = null;
            }
            return(flag);
        }