Exemplo n.º 1
0
 public DefaultSearchLocations()
 {
     _dictionary = new Dictionary <string, Pidl>(5);
     foreach (Environment.SpecialFolder folder in EnumerateLocations())
     {
         Pidl pidl = null;
         try
         {
             pidl = new Pidl(folder);
             string displayName = pidl.DisplayName;
             if (!string.IsNullOrEmpty(displayName))
             {
                 _dictionary.Add(displayName.ToLowerInvariant(), pidl);
                 continue;
             }
             pidl.Dispose();
         }
         catch (Exception)
         {
             if (pidl != null)
             {
                 pidl.Dispose();
             }
         }
     }
 }
Exemplo n.º 2
0
 public void Dispose()
 {
     if (_pidl != null)
     {
         _pidl.Dispose();
         _pidl = null;
     }
 }
Exemplo n.º 3
0
 public void BrowseToParent()
 {
     if (_browseHistory.Current != null)
     {
         Pidl parent = _browseHistory.Current.GetParent();
         if (parent != null)
         {
             try
             {
                 this.BrowseTo(parent);
             }
             finally
             {
                 parent.Dispose();
             }
         }
     }
 }
Exemplo n.º 4
0
        public ShellItem()
        {
            _rootItem = this;
            try
            {
                // create a PIDL for the Desktop shell item.
                _pidl = new Pidl(Environment.SpecialFolder.Desktop);

                var shInfo = new Native.SHFILEINFO();
                Native.Shell32.SHGetFileInfo((IntPtr)_pidl, 0, out shInfo, (uint)Marshal.SizeOf(shInfo), SHGFI.SHGFI_PIDL | SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_SYSICONINDEX);

                // get the root IShellFolder interface
                int hResult = Native.Shell32.SHGetDesktopFolder(ref _shellFolder);
                if (hResult != 0)
                {
                    Marshal.ThrowExceptionForHR(hResult);
                }

                _displayName   = shInfo.szDisplayName;
                _typeName      = string.Empty;
                _iconIndex     = shInfo.iIcon;
                _isFolder      = true;
                _isVirtual     = true;
                _hasSubFolders = true;
            }
            catch (Exception ex)
            {
                // if an exception happens during construction, we must release the PIDL now (remember, it's a pointer!)
                if (_pidl != null)
                {
                    _pidl.Dispose();
                }
                throw new Exception("Creation of the root namespace shell item failed.", ex);
            }

#if DEBUG
            _instanceCount++;
#endif
        }
Exemplo n.º 5
0
        public override void Reload()
        {
            Pidl selectedPidl = _folderTreeView.CurrentPidl.Clone();

            _suspendBeforeBrowse = true;
            try
            {
                foreach (FolderTreeNode node in _folderTreeView.Nodes)
                {
                    node.Reload();
                }

                _folderTreeView.BrowseTo(selectedPidl);
            }
            catch (Exception ex)
            {
                this.HandleBrowseException(ex);
            }
            finally
            {
                _suspendBeforeBrowse = false;
                selectedPidl.Dispose();
            }
        }
Exemplo n.º 6
0
        public ShellItem(Pidl pidl, ShellItem parentShellItem, bool relativePidl)
        {
            int hResult;

            _rootItem = parentShellItem._rootItem;
            try
            {
                IntPtr tempPidl;
                if (relativePidl)
                {
                    _pidl    = new Pidl(parentShellItem.Pidl, pidl);
                    tempPidl = (IntPtr)pidl;                      // use the relative one from parameters
                }
                else
                {
                    _pidl    = pidl.Clone();
                    tempPidl = (IntPtr)_pidl;                      // use the absolute one that we constructed just now
                }

                const Native.SHGFI flags = Native.SHGFI.SHGFI_PIDL                 // indicates that we're specifying the item by PIDL
                                           | Native.SHGFI.SHGFI_DISPLAYNAME        // indicates that we want the item's display name
                                           | Native.SHGFI.SHGFI_SYSICONINDEX       // indicates that we want the item's icon's index in the system image list
                                           | Native.SHGFI.SHGFI_ATTRIBUTES         // indicates that we want the item's attributes
                                           | Native.SHGFI.SHGFI_TYPENAME;          // indicates that we want the item's type name
                Native.SHFILEINFO shInfo = new Native.SHFILEINFO();
                Native.Shell32.SHGetFileInfo((IntPtr)_pidl, 0, out shInfo, (uint)Marshal.SizeOf(shInfo), flags);

                // read item attributes
                Native.SFGAO attributeFlags = (Native.SFGAO)shInfo.dwAttributes;

                // create the item's IShellFolder interface
                if ((attributeFlags & Native.SFGAO.SFGAO_FOLDER) != 0)
                {
                    Guid iidIShellFolder = new Guid(IID.IID_IShellFolder);
                    if (_pidl == _rootItem._pidl)
                    {
                        // if the requested PIDL is the root namespace (the desktop) we can't use the the BindToObject method, so get it directly
                        hResult = Native.Shell32.SHGetDesktopFolder(ref _shellFolder);
                    }
                    else
                    {
                        if (relativePidl)
                        {
                            hResult = (int)parentShellItem._shellFolder.BindToObject(tempPidl, IntPtr.Zero, ref iidIShellFolder, out _shellFolder);
                        }
                        else
                        {
                            hResult = (int)_rootItem._shellFolder.BindToObject(tempPidl, IntPtr.Zero, ref iidIShellFolder, out _shellFolder);
                        }
                    }

                    if (hResult != 0)
                    {
                        // some objects are marked as folders, but really aren't and thus cannot be bound to an IShellFolder
                        // log these events for future study, but it's not exactly something to be concerned about in isolated cases.
                        // Marshal.ThrowExceptionForHR(hResult);
                        if ((attributeFlags & Native.SFGAO.SFGAO_HASSUBFOLDER) == 0)
                        {
                            attributeFlags = attributeFlags & ~Native.SFGAO.SFGAO_FOLDER;
                        }
                    }
                }

                _displayName   = shInfo.szDisplayName;
                _typeName      = shInfo.szTypeName;
                _iconIndex     = shInfo.iIcon;
                _isFolder      = (attributeFlags & Native.SFGAO.SFGAO_FOLDER) != 0;
                _isVirtual     = (attributeFlags & Native.SFGAO.SFGAO_FILESYSTEM) == 0;
                _hasSubFolders = (attributeFlags & Native.SFGAO.SFGAO_HASSUBFOLDER) != 0;
            }
            catch (UnauthorizedAccessException ex)
            {
                // if an exception happens during construction, we must release the PIDL now (remember, it's a pointer!)
                var path = string.Empty;
                if (_pidl != null)
                {
                    path = _pidl.Path;
                    _pidl.Dispose();
                }
                throw new PathAccessException(path, ex);
            }
            catch (Exception ex)
            {
                // if an exception happens during construction, we must release the PIDL now (remember, it's a pointer!)
                if (_pidl != null)
                {
                    _pidl.Dispose();
                }
                throw new Exception("Creation of the specified shell item failed.", ex);
            }

#if DEBUG
            _instanceCount++;
#endif
        }