I recently just added directories and files to the treeView. How would I go about adding an icon for the folder nodes, and then a different icon for the file nodes? Thank you very much.
Printable View
I recently just added directories and files to the treeView. How would I go about adding an icon for the folder nodes, and then a different icon for the file nodes? Thank you very much.
Images are displayed against nodes in a TreeView the same way they are displayed against items in a ListView. You create an ImageList and assign it to the ImageList property of the TreeView. You add Images to the Images collection of the ImageList and then set the ImageIndex or ImageKey of each node to specify which Image gets displayed against it.
You'll need to add one Image for the folders (or one open and one closed if you want get fancy) and one for each file type. How those Images are created is completely up to you. You can add them at design time or run time. If you want to get the system icon for a file then you can call Icon.ExtractAssociatedIcon to get an Icon object and then call ToBitmap on that to get a Bitmap object that can be added to an ImageList.
Thank you. Here is my code so far:
But there is an error in the TreeView:Code:private static void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
ImageList myImageList = new ImageList();
myImageList.Images.Add(Properties.Resources.foldericon); //icon 0
myImageList.Images.Add(Properties.Resources.html_icon); //icon 1
treeView.ImageList = myImageList;
var stack = new Stack<TreeNode>();
var rootDirectory = new DirectoryInfo(path);
var node = new TreeNode(rootDirectory.Name) { Tag = rootDirectory };
stack.Push(node);
treeView.Nodes.Add(node);
while (stack.Count > 0)
{
var currentNode = stack.Pop();
var directoryInfo = (DirectoryInfo)currentNode.Tag;
foreach (var directory in directoryInfo.GetDirectories())
{
var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
currentNode.Nodes.Add(childDirectoryNode);
treeView.Nodes.Add(directory.Name, directory.Name, 0);
stack.Push(childDirectoryNode);
}
foreach (var file in directoryInfo.GetFiles("*.htm"))
{
currentNode.Nodes.Add(new TreeNode(file.Name));
treeView.Nodes.Add(file.Name, file.Name, 1);
}
}
}
Attachment 101337
'folder' is the root directory, with a subdirectory, 'sub', and then a file in the sub, 'index.html'. Those get added, but they get added again right below it.
A tree is an inherently recursive structure so, unless you have a compelling reason to do otherwise, it makes sense to use recursion to populate it, e.g.Sample usage:vb.net Code:
Private Function GetFileSystemNodes(folderPath As String) As TreeNode() Dim nodes As New List(Of TreeNode) Try For Each subfolderPath In Directory.GetDirectories(folderPath) nodes.Add(New TreeNode(Path.GetFileName(subfolderPath), 0, 0, GetFileSystemNodes(subfolderPath)) With {.Tag = subfolderPath}) Next For Each filePath In Directory.GetFiles(folderPath, "*.htm") nodes.Add(New TreeNode(Path.GetFileName(filePath), 1, 1) With {.Tag = filePath}) Next Catch ex As UnauthorizedAccessException 'Ignore inaccessible folder and move on. End Try Return nodes.ToArray() End FunctionCode:Dim rootFolderPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim rootNode As New TreeNode(rootFolderPath, 0, 0, GetFileSystemNodes(rootFolderPath)) With {.Tag = rootFolderPath}
TreeView1.Nodes.Add(rootNode)