Display directory structure in TreeView Just In Time in C#

No.of Views5511
Bookmarked1 times
Downloads 
Votes1
By  kirtan007   On  24 Jul 2010 10:07:47
Tag : CSharp , Windows Forms
In this article to show you how to display directory structure in TreeView Just in Time as Windows Explorer does.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 
Image Loading

Introduction

I am writing this article to show you how to display directory structure in TreeView Just in Time as Windows Explorer does. We will build sample application as below screen shot that will perform that thing

Technology

CSharp, .net 2.0/3.5/4.0

Implementation

One way to fill the directory structure is to scan through each directory and add nodes in tree view control but this might be time consuming as it takes a lot time to scan whole directory structure.

But here we will fill the tree with some other approach in which we will not scan whole file system at once but we will add node to tree as user clicks [+] Expand of the node that will speed up the process as it does not need to scan thorough whole file system.

So lets get started with our example.First drag drop one TreeView control on the form as shown in screenshot.

Here is the whole code . Just read the code I am explaining it in just at the ending about how its working.

using System.IO;
private void Form1_Load(object sender, EventArgs e)
{
    TreeNode rootnode = new TreeNode(@"C:\");
    treeView1.Nodes.Add(rootnode);
    FillChildNodes(rootnode);
    treeView1.Nodes[0].Expand();
}
void FillChildNodes(TreeNode node)
{try{
       DirectoryInfo dirs = new DirectoryInfo(node.FullPath);foreach (DirectoryInfo dir in dirs.GetDirectories())
       {
           TreeNode newnode = new TreeNode(dir.Name);
           node.Nodes.Add(newnode);
           newnode.Nodes.Add("*");
       }
   }catch(Exception ex)
   {
        MessageBox.Show(ex.Message.ToString());
   }
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{if (e.Node.Nodes[0].Text == "*")
   {
       e.Node.Nodes.Clear();
       FillChildNodes(e.Node);
   }
}

Explanation

In above code, in load event of the form we have taken one node as we want to add this node in TreeView as rootNode.

Now in second line we are calling function FillChildNodes() method that takes any TreeNode as argument and adds all child directories to the provided node. Here note that we are also adding one node that is  "*" this is because once we expand the node we will clear that node and fill child node of the node that we just expanded. This logic will fill child nodes only if it finds node "*" if its not there then no need to add child nodes under parent.

We written logic of adding child node in treeView1_BeforeExpand() event of the treeView1 so that as soon as user clicks Expand [+] it will check for that "*" node if it found then it will clear that "*" node and fill the child nodes.

In FillChildNode method we are also handling exception as we may some time encounter the exception if we don't have rights to access the directory so that we have handled the exception with try catch block

Conclusion

In this article we have seen how to show directory structure in TreeView control.

Sample Project Source

Download source files -38 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
By:TomDate Of Posted:2/29/2012 6:06:05 AM
does not work
Child nodes are never populated
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</