Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#

File Explorer using Treeview controller in C# 2.0

Rate me:
Please Sign up or sign in to vote.
2.91/5 (22 votes)
10 Mar 2006CPOL1 min read 190.1K   12.9K   35   23
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:

C#
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):

C#
this. treeView1.BeforeExpand += 
    new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
C#
this.Load += new System.EventHandler(this.Form1_Load);

Add the following methods to Form1.cs class:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
Member 1240197218-Mar-16 8:22
Member 1240197218-Mar-16 8:22 
QuestionProject Pin
Member 109201934-Sep-14 2:46
Member 109201934-Sep-14 2:46 
Questioncan use in c# 4.0 Pin
Member 1039243919-Jun-13 15:43
Member 1039243919-Jun-13 15:43 
GeneralMy vote of 5 Pin
musicm12227-Apr-11 10:44
professionalmusicm12227-Apr-11 10:44 
GeneralRe: My vote of 5 Pin
Anunay Inuganti6-Mar-13 0:12
Anunay Inuganti6-Mar-13 0:12 
GeneralI've put this in one file Pin
vanstrien16-Mar-10 13:29
vanstrien16-Mar-10 13:29 
GeneralRe: I've put this in one file Pin
Rayan Isran28-Oct-10 5:35
Rayan Isran28-Oct-10 5:35 
GeneralFiles and foldes in listview Pin
starapple18-Mar-10 9:54
starapple18-Mar-10 9:54 
Generalgr8 job Pin
shikhakenue28-Dec-08 20:49
shikhakenue28-Dec-08 20:49 
Questionhow do we display file folders in local system drives in tree view Pin
syamprasadg22-Dec-08 22:52
syamprasadg22-Dec-08 22:52 
Questionhow to automatically update contents of treeview Pin
Member 217919620-Dec-08 8:11
Member 217919620-Dec-08 8:11 
GeneralError found in File Explorer.cs Pin
sks050127-Mar-08 22:16
sks050127-Mar-08 22:16 
GeneralGood work... Pin
Tarik Guney28-Apr-07 22:12
Tarik Guney28-Apr-07 22:12 
QuestionHow do I copy the selected file to some other location using the API? Pin
sachinwable12325-Apr-07 4:24
sachinwable12325-Apr-07 4:24 
Generalpls reply i am a fresher with many confusion Pin
anoopsidharth8-Oct-06 19:09
anoopsidharth8-Oct-06 19:09 
GeneralA couple of errors.... Pin
ChristianHG11-Mar-06 7:39
ChristianHG11-Mar-06 7:39 
GeneralRe: A couple of errors.... Pin
Chandana Subasinghe11-Mar-06 7:41
Chandana Subasinghe11-Mar-06 7:41 
GeneralRe: A couple of errors.... Pin
alekcarlsen12-Aug-13 20:52
alekcarlsen12-Aug-13 20:52 
GeneralFolderBrowserDialog Pin
Marc Clifton10-Mar-06 2:21
mvaMarc Clifton10-Mar-06 2:21 
GeneralRe: FolderBrowserDialog Pin
shofb10-Mar-06 21:00
shofb10-Mar-06 21:00 
GeneralRe: FolderBrowserDialog Pin
Marc Clifton11-Mar-06 3:28
mvaMarc Clifton11-Mar-06 3:28 
Generalok but... Pin
toxcct10-Mar-06 2:08
toxcct10-Mar-06 2:08 
...where is the article body ??

where do you explain ?

GeneralRe: ok but... Pin
Chandana Subasinghe10-Mar-06 2:23
Chandana Subasinghe10-Mar-06 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.