Click here to Skip to main content
15,180,707 members
Articles / Programming Languages / C#
Posted 10 Mar 2006


35 bookmarked

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
A simple application I developed to display files and folders in your computer using Treeview controller in C# 2.0
Sample Image - TreeViewFileExplorer.jpg


This is a simple application that I developed to display files and folders in your computer using Treeview controller in C# 2.0.


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            

private void trwFileExplorer_BeforeExpand(object sender,  TreeViewCancelEventArgs e)
            if (e.Node.Nodes[0].Text == "")
                TreeNode node = fe.EnumerateDirectory(e.Node);


  • 10th March, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chandana Subasinghe
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

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