Introduction
In this tip, I am going to share with you how to Display Sub Directories and Files in Treeview in C#.
Here, we are going to learn to Display Sub directories and Files into TreeView and by using Tooltip on Mouse Hover, it will display the path of the file or folder. This
article will help you to understand the TreeView and TreeNode’s concepts.
Targeted Audiences
The targeted audience is people with basic knowledge of C#.
Explanation
Things to do:
Create a New Project and give it a suitable name as I gave the project name ‘DisplayDirectoryTreeview’.
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 1/7
20.12.2017 Display Sub Directories and Files in Treeview - CodeProject
Now, make a UI which contains two Buttons, one Textbox, one ProgressBar, a single TreeView, FolderBrowserDialog, ImageList and ToolTip
tool. So, here, our UI looks like the following:
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 2/7
20.12.2017 Display Sub Directories and Files in Treeview - CodeProject
Now, I will explain the functionality of these components which we used in our form. There is Textbox which we used to take User input for directory path, we have
used FolderBrowserDialog for the same purpose, ImageList to store the Folder and File Icons Images, Progressbar to showing the progress, after
selecting the Directory path, user can click ‘Load Directory’ button to load the selected directory.
Code
Hide Copy Code
using System.Windows.Forms;
using System.IO;
Code for the ‘Load Directory’ button click event. In this function, we are clearing the old data such resetting the progressbar value and clearing Treeview nodes
data and then we are calling our main function ‘LoadDirectory’.
In this function, we are passing a root directory path which we are getting from the textbox in function parameter and loading sub directories of the given path and
creating nodes for each respective directory, as shown in the below code:
In this function, we are passing a directory path in function parameter and loading sub directories of the given path and creating nodes for each respective directory
and setting Image for the directory node, as shown in the below code:
In this function, we are passing a directory path in function parameter and loading files of the given path and creating nodes for each respective file and setting Image
for the file node, as shown in the below code:
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 3/7
20.12.2017 Display Sub Directories and Files in Treeview - CodeProject
foreach (string file in Files)
{
FileInfo fi = new FileInfo(file);
TreeNode tds = td.Nodes.Add(fi.Name);
tds.Tag = fi.FullName;
tds.StateImageIndex = 1;
UpdateProgress();
}
}
In ‘UpdateProgress’ function, we are simply increasing the progressbar value and on the basis of current progressbar value and maximum value, we are
displaying the total progress in percentage.
Code for the Mouse Move event, here we are applying tooltip to each TreeView node, first we are finding the exact location of mouse and on the basis of location of
TreeView, we are finding the TreeNode, and displaying the TreeNode Tag data in tooltip.
Hide Copy Code
Output
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 4/7
20.12.2017 Display Sub Directories and Files in Treeview - CodeProject
Conclusion
By using these easy and simple methods, we can load a particular directory data in TreeView, these simple tips may help a beginner level reader to understand the
concepts of TreeView and TreeNodes and also there is no direct option to display tooltip for each node in TreeView so here you will get the easiest method of
tooltip for Treeview Nodes. And also displaying image icon in TreeNode.
Hope this will help you and you would like this article.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
TWITTER FACEBOOK
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 5/7
20.12.2017 Display Sub Directories and Files in Treeview - CodeProject
Altaf Ansari
Software Developer Prothious Engineering Services
India
I am Dot Net Developer and Working on Microsoft Technology Asp.Net, C#, SQL, Windows Application, Web Application.
Enhanced BrowseForFolder styled TreeView Window Tabs (WndTabs) Add-In for DevStudio
Displaying XML in a WPF TreeView To Heap or not to Heap; That’s the Large Object Question?
Search Comments
Great
Member 13560289 5-Dec-17 16:15
Display Sub Directories and Files in Treeview is great, I got somuch nice information.
Exolearn!
Sign In · View Thread
Re: Great
Altaf Ansari 5-Dec-17 18:23
Thank You...
Sign In · View Thread
You'd be better off asynchronously searching for files and folders and populating the tree control via event handlers. To understand why use of
Application.DoEvents() isn't recommend, see the Caution note on this[^] MSDN page.
/ravi
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 6/7
20.12.2017 Display Sub Directories and Files in Treeview - CodeProject
My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
ravib(at)ravib(dot)com
Sign In · View Thread
Sign In · View Thread
Refresh 1
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | Article Copyright 2017 by Altaf Ansari
Sprache auswählen ▼
Web03 | 2.8.171207.1 | Last Updated 5 Dec 2017 fluid Everything else Copyright © CodeProject, 1999-2017
https://www.codeproject.com/Tips/1219576/Display-Sub-Directories-and-Files-in-Treeview 7/7