Click here to Skip to main content
14,550,977 members

File Explorer using Treeview controller in C# 2.0

Rate this:
2.91 (22 votes)
Please Sign up or sign in to vote.
2.91 (22 votes)
10 Mar 2006CPOL
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chandana Subasinghe
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionThanks Pin
Member 1240197218-Mar-16 8:22
MemberMember 1240197218-Mar-16 8:22 
QuestionProject Pin
Member 109201934-Sep-14 2:46
MemberMember 109201934-Sep-14 2:46 
Questioncan use in c# 4.0 Pin
Member 1039243919-Jun-13 15:43
MemberMember 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
MemberAnunay Inuganti6-Mar-13 0:12 
GeneralI've put this in one file Pin
vanstrien16-Mar-10 13:29
Membervanstrien16-Mar-10 13:29 
GeneralRe: I've put this in one file Pin
Rayan Isran28-Oct-10 5:35
MemberRayan Isran28-Oct-10 5:35 
GeneralFiles and foldes in listview Pin
starapple18-Mar-10 9:54
Memberstarapple18-Mar-10 9:54 
Generalgr8 job Pin
shikhakenue28-Dec-08 20:49
Membershikhakenue28-Dec-08 20:49 
Questionhow do we display file folders in local system drives in tree view Pin
syamprasadg22-Dec-08 22:52
Membersyamprasadg22-Dec-08 22:52 
Questionhow to automatically update contents of treeview Pin
Member 217919620-Dec-08 8:11
MemberMember 217919620-Dec-08 8:11 
GeneralError found in File Explorer.cs Pin
sks050127-Mar-08 22:16
Membersks050127-Mar-08 22:16 
GeneralGood work... Pin
Tarik Guney28-Apr-07 22:12
MemberTarik 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
Membersachinwable12325-Apr-07 4:24 
Generalpls reply i am a fresher with many confusion Pin
anoopsidharth8-Oct-06 19:09
Memberanoopsidharth8-Oct-06 19:09 
GeneralA couple of errors.... Pin
ChristianHG11-Mar-06 7:39
MemberChristianHG11-Mar-06 7:39 
GeneralRe: A couple of errors.... Pin
Chandana Subasinghe11-Mar-06 7:41
MemberChandana Subasinghe11-Mar-06 7:41 
GeneralRe: A couple of errors.... Pin
alekcarlsen12-Aug-13 20:52
Memberalekcarlsen12-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
Membershofb10-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
Membertoxcct10-Mar-06 2:08 
GeneralRe: ok but... Pin
Chandana Subasinghe10-Mar-06 2:23
MemberChandana 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.

Article
Posted 10 Mar 2006

Stats

173.4K views
12.1K downloads
35 bookmarked