Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File Explorer using Treeview controller in C# 2.0

0.00/5 (No votes)
10 Mar 2006 1  
A simple application I developed to display files and folders in your computer using Treeview controller in C# 2.0
Sample Image - TreeViewFileExplorer.jpg

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.

Sample screenshot

§ 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)
        {
         // Create file tree            
            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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here