Exemple #1
0
        /// <summary>
        /// Returns the desktop relative pIDL of a special folder
        /// </summary>
        /// <param name="desktopIShellFolder">The IShellFolder instance of the Desktop</param>
        /// <param name="csidl">The CSIDL of the special folder</param>
        /// <returns>the desktop relative pIDL of a special folder</returns>
        public static IntPtr initSpecialPIDL(Object desktopIShellFolder, int csidl)
        {
            IntPtr result = new IntPtr();

            ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, (ShellApi.CSIDL)csidl, ref result);
            return(result);
        }
 private void BecomeComputerBrowser()
 {
     _uiFlags   += BrowseFlags.BIF_BROWSEFORCOMPUTER;
     Description = "Select a computer:";
     ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, ShellApi.CSIDL.CSIDL_NETWORK, ref this._rootFolderLocation);
     ShowNewFolderButton = false;
     ShowEditBox         = false;
 }
Exemple #3
0
        /// <summary>
        /// Returns the pIDL of the desktop itself
        /// </summary>
        public static IntPtr initDesktopPIDL()
        {
            IntPtr pidl = new IntPtr();

            // get the root shell folder
            ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, ShellApi.CSIDL.CSIDL_DESKTOP, ref pidl);

            return(pidl);
        }
Exemple #4
0
        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);
        }