Click here to Skip to main content
11,494,851 members (38,512 online)
Click here to Skip to main content

Filesystem TreeView

, 8 Jul 2005 125.6K 1.9K 58
Rate this:
Please Sign up or sign in to vote.
A filesystem treeview for .NET.

Screenshot

Introduction

There is no file system treeview in .NET 1.1! Therefore I ended up making my own. This is a very basic version of a file system treeview. There are a lot of other methods and or properties that could be added in order to make this a more useful control. However, I have posted this control with the intention that this will save someone some time and aggravation.

Performance is often an issue when dealing with recursion and an extremely large file system. Therefore to overcome this issue I designed this treeview component so it loads on demand. In order to make this happen, I initially only load the root directories and files. Obviously a file can not have any child nodes but a directory on the other hand can have sub-directories and files within it. If a directory has sub-directories and/or files, I add what I describe as a "fake child node". Basically all this means is that I add a child tree node to the "directory node" in order to display the "+" plus sign in front of it. This indicates to the user that they can drill down further to see the sub-directories and files. Here is some example code:

int fileCount = 0;

if( this.TreeView.ShowFiles == true )
    //get a file count
    fileCount = this._directoryInfo.GetFiles().Length;         

//if the directory contains 
//sub-items then add a fake child node to 
//indicate to the user that they can drill down
if( (fileCount + this._directoryInfo.GetDirectories().Length) > 0 )
    new FakeChildNode( this );

The FakeChildNode class is extremely simple. It derives from the TreeNode class and only has a single constructor that adds a child node to an existing node.

public class FakeChildNode : TreeNode
{
    public FakeChildNode( TreeNode parent ) : base()
    {
        parent.Nodes.Add( this );
    }
}

So now that we have the initial directories loaded and virtualized, we can write the code that controls the on-demand loading of the subnodes. In order to do this, I wrote an event handler for the "BeforeExpand" event of the treeview.

void FileSystemTreeView_BeforeExpand(object sender, 
                           TreeViewCancelEventArgs e)
{
    //if node is of type filenode then get out of event
    if( e.Node is FileNode ) return;
        
    DirectoryNode node = (DirectoryNode)e.Node;

    //checks to see if the node has already 
    //been loaded. Basically this just checks to 
    //see if the first child is of type "FakeChildNode"
    if (!node.Loaded)
    {
        //remove the fake child node used for virtualization
        node.Nodes[0].Remove();
        //Load sub-directories and files
        node.LoadDirectory();
        if( this._showFiles == true )
        node.LoadFiles();
    }
}

So now that you understand the basic logistics of this control, here is an example of how you can put it to use:

Using the code

C2C.FileSystem.FileSystemTreeView tree = 
             new C2C.FileSystem.FileSystemTreeView();         
Controls.Add( tree );
tree.Dock = DockStyle.Fill;
//if you want to view only folders 
//you can set the ShowFiles property to true
//tree.ShowFiles = false; 
tree.Load( @"C:\" );

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

Share

About the Author

Michael Ceranski
Software Developer (Senior) Concepts2Code
United States United States
Michael is the co-founder and master consultant for Concepts2Code, a software consulting company based in Buffalo, New York. He's been programming since the early 1990's. His vast programming experience includes VB, Delphi, C#, ASP, ASP.NET, Ruby on Rails, Coldfusion and PHP. Michael also is a Microsoft Certified Application Developer and a Certified Technology Specialist for SQL Server.

Visit his blog.

Comments and Discussions

 
QuestionDisplay System Folders Pin
wynnej198324-Apr-07 20:59
memberwynnej198324-Apr-07 20:59 
QuestionQueston about the selected node full path Pin
cervanteskbron17-Apr-07 12:00
membercervanteskbron17-Apr-07 12:00 
QuestionGreat work Pin
Boris Modylevsky22-Oct-06 1:04
memberBoris Modylevsky22-Oct-06 1:04 
GeneralRe: Great work - Solution: ColorDepth Pin
Assaad Awada28-Apr-08 5:25
memberAssaad Awada28-Apr-08 5:25 
Questionhow to load the selectedfile int to the browser Pin
xall13-Aug-06 8:52
memberxall13-Aug-06 8:52 
Questionhow to open a file Pin
xall30-Jul-06 7:21
memberxall30-Jul-06 7:21 
AnswerRe: how to open a file Pin
Michael Ceranski31-Jul-06 15:41
memberMichael Ceranski31-Jul-06 15:41 
GeneralSome small additions I made - Return the filename selected Pin
Jab_RTS11-Jul-06 19:57
memberJab_RTS11-Jul-06 19:57 
GeneralRe: Some small additions I made - Return the filename selected Pin
xall30-Jul-06 7:23
memberxall30-Jul-06 7:23 
GeneralFanta Fantastic Pin
D. Emilio Grimaldo Tuñon29-Jun-06 10:51
memberD. Emilio Grimaldo Tuñon29-Jun-06 10:51 
GeneralDoesn't Compile Pin
Tom Dane3-Jun-06 18:27
memberTom Dane3-Jun-06 18:27 
GeneralSearch Filter Pin
holeinmypocket7-Feb-06 5:50
memberholeinmypocket7-Feb-06 5:50 
GeneralRe: Search Filter Pin
alejo_rybak2-Oct-06 12:46
memberalejo_rybak2-Oct-06 12:46 
GeneralPopulate on every expand Pin
Michael Pitoniak13-Jan-06 2:05
memberMichael Pitoniak13-Jan-06 2:05 
QuestionGet files to open in their associated app? Pin
fatJ24-Aug-05 10:34
memberfatJ24-Aug-05 10:34 
AnswerRe: Get files to open in their associated app? Pin
mceranski24-Aug-05 10:38
membermceranski24-Aug-05 10:38 
GeneralRe: Get files to open in their associated app? Pin
fatJ25-Aug-05 3:37
memberfatJ25-Aug-05 3:37 
GeneralRe: Get files to open in their associated app? Pin
dreamer8125-Aug-05 4:05
memberdreamer8125-Aug-05 4:05 
Generalextended version with drag and drop Pin
dreamer8113-Aug-05 5:12
memberdreamer8113-Aug-05 5:12 
GeneralRe: extended version with drag and drop Pin
Michael Ceranski15-Aug-05 3:49
memberMichael Ceranski15-Aug-05 3:49 
GeneralRe: extended version with drag and drop Pin
dreamer8116-Aug-05 6:35
memberdreamer8116-Aug-05 6:35 
GeneralRe: extended version with drag and drop Pin
dreamer8117-Aug-05 23:38
memberdreamer8117-Aug-05 23:38 
GeneralRe: extended version with drag and drop Pin
dreamer8118-Aug-05 0:20
memberdreamer8118-Aug-05 0:20 
GeneralRe: extended version with drag and drop Pin
Michael Pitoniak13-Jan-06 2:06
memberMichael Pitoniak13-Jan-06 2:06 
QuestionSolution File? Pin
george338027-Jul-05 4:55
membergeorge338027-Jul-05 4:55 
AnswerRe: Solution File? Pin
dreamer8113-Aug-05 5:32
memberdreamer8113-Aug-05 5:32 
Generalgood work but Pin
eRRaTuM6-Jul-05 2:02
membereRRaTuM6-Jul-05 2:02 
GeneralRe: good work but Pin
mceranski11-Jul-05 8:46
membermceranski11-Jul-05 8:46 
GeneralGood work Pin
Aryasheel28-Jun-05 22:25
memberAryasheel28-Jun-05 22:25 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 8 Jul 2005
Article Copyright 2005 by Michael Ceranski
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid