private static void AddRootNode(TreeView tree, ref int imageCount, ImageList imageList, ShellFolder shellFolder, bool getIcons) { Shell32.Shell shell32 = new Shell32.ShellClass(); Shell32.Folder shell32Folder = shell32.NameSpace(shellFolder); Shell32.FolderItems items = shell32Folder.Items(); tree.Nodes.Clear(); TreeNode desktop = new TreeNode("Desktop", 0, 0); // Added in version 1.11 // add a FolderItem object to the root (Desktop) node tag that corresponds to the DesktopDirectory namespace // This ensures that the GetSelectedNodePath will return the actual Desktop folder path when queried. // There's possibly a better way to create a Shell32.FolderItem instance for this purpose, // but I surely don't know it Shell32.Folder dfolder = shell32.NameSpace(ShellFolder.DesktopDirectory); foreach(Shell32.FolderItem fi in dfolder.ParentFolder.Items()) { if(fi.Name == dfolder.Title) { desktop.Tag = fi; break; } } // Add the Desktop root node to the tree tree.Nodes.Add(desktop); // iterate through the Desktop namespace and populate the first level nodes foreach(Shell32.FolderItem item in items) { if(item.IsFolder) // this ensures that desktop shortcuts etc are not displayed { TreeNode tn = AddTreeNode(item, ref imageCount, imageList, getIcons); desktop.Nodes.Add(tn); CheckForSubDirs(tn, imageList); } } }
/// <summary> /// Creates a ShellFileSystemObject given a native IShellItem interface /// </summary> /// <param name="nativeShellItem"></param> /// <returns>A newly constructed ShellFileSystemObject object</returns> internal static new ShellFileSystemObject FromShellItem(IShellItem nativeShellItem) { string path = null; // Get the full system path from the native IShellItem path = ShellHelper.GetParsingName(nativeShellItem); if (string.IsNullOrEmpty(path)) throw new ArgumentException("Cannot create a ShellFileSystemObject from", "nativeShellItem"); ShellFileSystemObject item = null; if (File.Exists(path)) { item = new ShellFile(); } else if (Directory.Exists(path)) { item = new ShellFolder(); } else if (path.StartsWith("::")) // It's a special folder, we'll use it { //TODO: Construct a special type for registered non-folder Shell items item = new ShellFile(); } else { return null; // There aren't any other options left } item.Path = path; item.nativeShellItem = (IShellItem2)nativeShellItem; return item; }
private static void AddRootNode(TreeView tree, ref int imageCount, ImageList imageList, ShellFolder shellFolder, bool getIcons) { Shell32.Shell shell32 = new Shell32.ShellClass(); Shell32.Folder shell32Folder = shell32.NameSpace(shellFolder); imageCount = AddRootNode(tree, imageCount, imageList, getIcons, shell32, shell32Folder, "Desktop"); }
private static void AddRootNode( TreeView tree, ref int imageCount, ImageList imageList, ShellFolder shellFolder, bool getIcons ) { Shell32.Shell shell32 = new Shell32.ShellClass(); Shell32.Folder shell32Folder = shell32.NameSpace( shellFolder ); Shell32.FolderItems items = shell32Folder.Items(); tree.Nodes.Clear(); TreeNode desktop = new TreeNode( "Desktop", 0, 0 ); // Added in version 1.11 // add a FolderItem object to the root (Desktop) node tag that corresponds to the DesktopDirectory namespace // This ensures that the GetSelectedNodePath will return the actual Desktop folder path when queried. // There's possibly a better way to create a Shell32.FolderItem instance for this purpose, // but I surely don't know it Shell32.Folder dfolder = shell32.NameSpace( ShellFolder.DesktopDirectory ); foreach( Shell32.FolderItem fi in dfolder.ParentFolder.Items() ) { if( fi.Name == dfolder.Title ) { desktop.Tag = fi; break; } } // Add the Desktop root node to the tree tree.Nodes.Add( desktop ); // [xiperware] Get FolderItem that represents Recycle Bin Shell32.Folder recFolder = shell32.NameSpace( ShellFolder.RecycleBin ); Shell32.FolderItem recycle = null; foreach( Shell32.FolderItem fi in recFolder.ParentFolder.Items() ) { if( fi.Name == recFolder.Title ) { recycle = fi; break; } } // [xiperware] Get FolderItem that represents My Network Places Shell32.Folder netFolder = shell32.NameSpace( ShellFolder.NetworkNeighborhood ); Shell32.FolderItem mynetwork = null; foreach( Shell32.FolderItem fi in netFolder.ParentFolder.Items() ) { if( fi.Name == netFolder.Title ) { mynetwork = fi; break; } } // iterate through the Desktop namespace and populate the first level nodes foreach( Shell32.FolderItem item in items ) { if( !item.IsFolder ) continue; // this ensures that desktop shortcuts etc are not displayed if( item.IsBrowsable ) continue; // [xiperware] exclude zip files if( recycle != null && item.Path == recycle.Path ) continue; // [xiperware] skip recycle bin TreeNode tn = AddTreeNode( item, ref imageCount, imageList, getIcons ); desktop.Nodes.Add( tn ); if( mynetwork != null && item.Path == mynetwork.Path ) // [xiperware] skip my network places subdirs { tree.Tag = tn; // store node in tag continue; } CheckForSubDirs( tn, imageList ); } }