Click here to Skip to main content
Licence CPOL
First Posted 10 Mar 2006
Views 95,118
Downloads 4,257
Bookmarked 30 times

File Explorer using Treeview controller in C# 2.0

By | 10 Mar 2006 | Article
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)

About the Author

Chandana Subasinghe

Web Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberTerranceSmith10:44 27 Apr '11  
GeneralI've put this in one file Pinmembervanstrien13:29 16 Mar '10  
GeneralRe: I've put this in one file Pinmemberleosco5:35 28 Oct '10  
GeneralFiles and foldes in listview Pinmemberstarapple19:54 8 Mar '10  
Generalgr8 job Pinmembershikhakenue20:49 28 Dec '08  
Questionhow do we display file folders in local system drives in tree view Pinmembersyamprasadg22:52 22 Dec '08  
Questionhow to automatically update contents of treeview PinmemberMember 21791968:11 20 Dec '08  
GeneralError found in File Explorer.cs Pinmembersks050122:16 27 Mar '08  
GeneralGood work... Pinmemberatarikg22:12 28 Apr '07  
QuestionHow do I copy the selected file to some other location using the API? Pinmembersachinwable1234:24 25 Apr '07  
Generalpls reply i am a fresher with many confusion Pinmemberoopz0519:09 8 Oct '06  
GeneralA couple of errors.... PinmemberChristianHG7:39 11 Mar '06  
GeneralRe: A couple of errors.... PinmemberChandana Subasinghe7:41 11 Mar '06  
GeneralFolderBrowserDialog PinprotectorMarc Clifton2:21 10 Mar '06  
GeneralRe: FolderBrowserDialog Pinmembershofb21:00 10 Mar '06  
GeneralRe: FolderBrowserDialog PinprotectorMarc Clifton3:28 11 Mar '06  
Generalok but... Pinmemberv2.02:08 10 Mar '06  
GeneralRe: ok but... PinmemberChandana Subasinghe2:23 10 Mar '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Mar 2006
Article Copyright 2006 by Chandana Subasinghe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid