/// <summary> /// Initialises the specified parent folder. /// </summary> /// <param name="parentFolder">The parent folder.</param> public void Initialise(ShellItem parentFolder) { // If we're not double buffered, we want to be. if (!DoubleBuffered) { DoubleBuffered = true; } // Clear children. Items.Clear(); // Clear the map. itemsToFolders.Clear(); // Work out the types to show. var childTypes = ChildTypes.Files; if (ShowHiddenFilesAndFolders) { childTypes |= ChildTypes.Hidden; } if (ShowFolders) { childTypes |= ChildTypes.Folders; } // Go through the children. foreach (var child in parentFolder.GetChildren(childTypes)) { // Do we need to create the icon for the item? if (shellIconIndexesToCacheIconIndexes.ContainsKey(child.IconIndex) == false) { // Get the shell icon for the item. var smallIcon = Icon.FromHandle(ComCtl32.ImageList_GetIcon(ShellImageList.GetImageList(ShellImageListSize.Small), child.IconIndex, 0)); var largeIcon = Icon.FromHandle(ComCtl32.ImageList_GetIcon(ShellImageList.GetImageList(ShellImageListSize.Large), child.IconIndex, 0)); // Create it and add it. var mappedIndex = SmallImageList.Images.Count; SmallImageList.Images.Add(smallIcon); LargeImageList.Images.Add(largeIcon); shellIconIndexesToCacheIconIndexes[child.IconIndex] = mappedIndex; } // Create an item. var item = new ListViewItem { Text = child.DisplayName, ImageIndex = shellIconIndexesToCacheIconIndexes[child.IconIndex] }; // Map it. itemsToFolders[item] = child; // Insert the item. Items.Add(item); // Fire the event handler. FireOnShellItemAdded(item); } }
static IEnumerable <TreeNode> EnumerateUnsafe(ShellItem shellFolder, ChildTypes childFlags, TreeNode node, IDictionary <TreeNode, ShellItem> nodesToFolders) { var numer = shellFolder.GetChildren(childFlags, false).GetEnumerator(); while (numer.MoveNext()) { ShellItem child = numer.Current; // Create a child node. var childNode = new TreeNode { Text = child.DisplayName, ImageIndex = child.IconIndex, SelectedImageIndex = child.IconIndex, }; // Map the node to the shell folder. nodesToFolders[childNode] = child; // If this item has children, add a child node as a placeholder. if (child.HasSubFolders) { childNode.Nodes.Add(string.Empty); } // Add the child node. node.Nodes.Add(childNode); yield return(childNode); } }