Introduction
This is a simple application that I developed to display files and folders in your computer using Treeview
controller in C# 2.0.
Background
I saw on the internet that a lot of developers had asked how to load files and folders of their computer using .NET Treeview
controller. At the same time, I saw there is a new feature with .NET 2.0 to get the information of the special folders like Desktop, My Documents, etc. Therefore I thought of building this very simple application and class.
System Requirements
- Windows 98, Window 2000, XP, NT, 2003
- Microsoft .NET Framework 2.0
Features of This Application
- Automatically detects and loads Drives, Folders, files, Desktop, etc.
- Easy to use this class for any application
- Easily extensible
Using the Code
§ Add Classes to Your Application
If you want to use this code, what you have to do is create a Windows application using C# 2.0 and copy the “FileExplorer.cs” class into your application. Then add the Treeview
controller into Form1
.
§ Add ImageList Controller
If you need to add pictures into the Nodes add Image controller into the Form1
, then add images into that as shown below. Make sure to add in the same order.
§ Initialize Variables
Then create a class level private
variable in the Form1.cs as follows:
FileExplorer fe = new FileExplorer();
§ Reacting to Event
You need to add two events into the application. Add the following delegates into Form1.Designer.cs file (Partial class of the Form1.cs):
this. treeView1.BeforeExpand +=
new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.Load += new System.EventHandler(this.Form1_Load);
Add the following methods to Form1.cs class:
private void Form1_Load(object sender, EventArgs e)
{
fe.CreateTree(this.trwFileExplorer);
}
private void trwFileExplorer_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Nodes[0].Text == "")
{
TreeNode node = fe.EnumerateDirectory(e.Node);
}
}
History
- 10th March, 2006: Initial post