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 }
private void Browse(ShellItem destination) { if (_currentShellItem != destination) { CancelEventArgs e = new CancelEventArgs(); if (!_suppressBrowseEvents && this.BeforeBrowse != null) this.BeforeBrowse.Invoke(this, e); if (e.Cancel) return; this.SuspendLayout(); try { DisposeEach(this.Items); this.Items.Clear(); if (_currentShellItem != null) _currentShellItem.Dispose(); _currentShellItem = destination; if (_currentShellItem != null) this.PopulateItems(); } catch (PathNotFoundException ex) { HandleBrowseException(ex); } catch (Exception ex) { HandleBrowseException(new IOException("The specified path is inaccessible.", ex)); } finally { this.ResumeLayout(true); } if (!_suppressBrowseEvents && this.AfterBrowse != null) this.AfterBrowse.Invoke(this, EventArgs.Empty); } }
protected override void Dispose(bool disposing) { if (disposing) { DisposeEach(this.Items); if (_currentShellItem != null) { _currentShellItem.Dispose(); _currentShellItem = null; } if (_rootShellItem != null) { _rootShellItem.Dispose(); _rootShellItem = null; } if (_myDocumentsReferencePidl != null) { _myDocumentsReferencePidl.Dispose(); _myDocumentsReferencePidl = null; } } base.Dispose(disposing); }
public FolderListView() : base() { _currentShellItem = _rootShellItem.Clone(); _myDocumentsReferencePidl = new Pidl(Environment.SpecialFolder.MyDocuments); this.Alignment = ListViewAlignment.SnapToGrid; this.AllowColumnReorder = false; this.AutoArrange = true; this.CheckBoxes = false; this.DereferenceLinks = true; this.HeaderStyle = ColumnHeaderStyle.Clickable; this.HideSelection = false; this.LabelEdit = false; this.LabelWrap = true; this.ListViewItemSorter = new FolderListViewItemComparer(); this.MultiSelect = true; this.Scrollable = true; this.Sorting = SortOrder.Ascending; this.TileSize = new Size(12*(this.FontHeight + 4), 3*this.FontHeight + 4); this.View = View.LargeIcon; this.PopulateItems(); }
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 (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 }
public ShellItem(Pidl pidl, ShellItem parentShellItem) : this(pidl, parentShellItem, true) {}
protected virtual void Dispose(bool disposing) { if (!_disposed) { #if DEBUG --_instanceCount; #endif if (disposing) { if (_pidl != null) { _pidl.Dispose(); _pidl = null; } if (_rootItem != null) { // do not dispose this - we don't own it! _rootItem = null; } } if (_shellFolder != null) { // release the IShellFolder interface of this shell item Marshal.ReleaseComObject(_shellFolder); _shellFolder = null; } _disposed = true; } }
protected override void Dispose(bool disposing) { if (disposing) { DisposeEach(base.Nodes); // disposing our top level nodes will also dispose the root shell item for us _rootShellItem = null; } base.Dispose(disposing); }
public void Dispose() { DisposeEach(base.Nodes); if (_shellItem != null) { _shellItem.Dispose(); _shellItem = null; } }
public FolderTreeNode(ShellItem shellItem) : base() { _shellItem = shellItem; this.Text = _shellItem.DisplayName; this.ImageIndex = _shellItem.IconIndex; this.SelectedImageIndex = _shellItem.IconIndex; // if this folder item has children then add a place holder node. if (_shellItem.IsFolder && _shellItem.HasSubFolders) this.Nodes.Add(new FolderTreeNode()); }