Click here to Skip to main content
15,890,043 members
Articles / Programming Languages / XML

FolderTreeView Control

Rate me:
Please Sign up or sign in to vote.
4.87/5 (33 votes)
25 Oct 20023 min read 392.1K   5K   129   107
A simple explorer-like FolderTreeView control for C#

Sample Image

Introduction

This is an all-new version of the FolderTreeView control I posted here at CodeProject some weeks ago. The control now starts in the Desktop namespace, and a new drilling method has been added so the startup folder can be specified. Please note that this control is not intended to have all of the functionality of the actual Windows Explorer TreeView - it is a light-weight control designed for use in projects where you want to supply a treeview for folder navigation, without supporting windows shell extensions. If you are looking for a control that supports shell extensions, you should be looking at the excellent ËxplorerTreeControl submitted by Carlos H Perez here at the CodeProject website.

How It Works

After calling the InitFolderTreeView() method, a dummy Desktop note is created as the root node, and then the shell's Desktop namespace is iterated to populate the first level nodes. The child nodes are then iterated, checking only for the presence of sub-child nodes. If sub-child nodes are found, a dummy node is inserted so that the [+] will be displayed for folders that can be expanded. When the BeforeExpand event fires, the folder being expanded is checked for a dummy node. If a dummy is present, it is removed and the node's children are re-populated. If the node's children have already been populated, no further action occurs.

Usage

The FolderTreeView control is based on 3 classes:

  • FolderTreeView - The FolderTreeView implementation
  • ShellOperations - Supporting Shell Operations
  • ExtractIcons - Shell Icon Extractor

The control naturally derives from the System.Windows.Forms.TreeView, exposing 1 new property, GetSelectedNodePath() which returns the currently selected node's full path. There are 2 new methods: InitFolderTreeView() must be called to populate the TreeView - normally, you would call this in your Form's OnLoad event. The 2nd method is DrillToFolder(string folderPath) - this will recurse through the tree looking for the specified folder path, returning a boolean to indicate if the search was successful or not.

The method used to retrieve the icon for the Desktop root node works fine, but is perhaps not the best way to go about it. This control has NOT been tested in Win9x, NT or 2000 - feedback on these OSs would be much appreciated. Of course, if you improve this control, I would appreciate the update, and if you have any feedback to offer, please post it below.

Updates

Version 1.11

  • Updated the GetDesktopIcon() method so it returns the shell's small desktop icon rather than the large version
  • Added code so that the Desktop node would return the full desktop folder path

I have now tested the code on Windows Me without problem - feedback on how it works with other OSs would be appreciated!

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.


Written By
Web Developer
Thailand Thailand
Furty will code for food.

Comments and Discussions

 
GeneralFile Names Pin
jpadova19-Feb-04 7:07
jpadova19-Feb-04 7:07 
GeneralRe: File Names Pin
IndyJason792-Jan-05 18:02
IndyJason792-Jan-05 18:02 
GeneralTreeViewCancelEventHandler behavior Pin
Marc Clifton17-Aug-03 15:16
mvaMarc Clifton17-Aug-03 15:16 
GeneralRe: TreeViewCancelEventHandler behavior Pin
Furty17-Aug-03 18:47
Furty17-Aug-03 18:47 
GeneralRe: TreeViewCancelEventHandler behavior Pin
Marc Clifton18-Aug-03 2:26
mvaMarc Clifton18-Aug-03 2:26 
GeneralRe: TreeViewCancelEventHandler behavior Pin
jmcpher26-Aug-03 16:17
jmcpher26-Aug-03 16:17 
GeneralPerformance and Shell32.dll questions Pin
Marc Clifton3-Aug-03 7:32
mvaMarc Clifton3-Aug-03 7:32 
GeneralRe: Performance and Shell32.dll questions Pin
Furty3-Aug-03 14:46
Furty3-Aug-03 14:46 
Hi Marc, the 3 major performance limitations of this code are:

1) Heavy use of interop
2) On loading, it scans CD/DVD drives to check for root folders
3) Heavy use of interop

I have made some substantial improvements to this code since it was published here, but I just haven't the time to update the article - you'd as good as anyone how much time it takes to write an article for CP.. Frown | :( I hope to get around to it within the next few weeks, but can't make any promises.

On the subject of shell32.dll binding, I don't quite follow - AFAIK the generated Interop.Shell32.dll should not be dependant on any particular Shell32 version, and my code doesn't call any Shell32 methods that don't exist in early versions. Basically, if the system can run .Net it should be able to run this code as is - have you found otherwise?

GeneralRe: Performance and Shell32.dll questions Pin
Furty3-Aug-03 16:29
Furty3-Aug-03 16:29 
GeneralRe: Performance and Shell32.dll questions Pin
Marc Clifton9-Aug-03 11:16
mvaMarc Clifton9-Aug-03 11:16 
GeneralRe: Performance and Shell32.dll questions Pin
Jim Wiese (aka Spunk)15-Sep-03 5:17
Jim Wiese (aka Spunk)15-Sep-03 5:17 
GeneralRe: Performance and Shell32.dll questions Pin
Member 26118820-Sep-03 23:55
Member 26118820-Sep-03 23:55 
GeneralRe: Performance and Shell32.dll questions Pin
Jonathan Gauthier23-Sep-03 10:04
Jonathan Gauthier23-Sep-03 10:04 
GeneralRe: Performance and Shell32.dll questions Pin
Member 26118823-Sep-03 10:17
Member 26118823-Sep-03 10:17 
GeneralRe: Performance and Shell32.dll questions Pin
IndyJason791-Jan-05 21:46
IndyJason791-Jan-05 21:46 
GeneralDesign View Pin
JPark31-Jul-03 22:12
JPark31-Jul-03 22:12 
GeneralMistakes in: Getting the FolderItem... Pin
ittay ophir22-Jul-03 3:43
ittay ophir22-Jul-03 3:43 
GeneralGetting the FolderItem Object From the Folder Interface Pin
ittay ophir21-Jul-03 0:38
ittay ophir21-Jul-03 0:38 
GeneralRe: Getting the FolderItem Object From the Folder Interface Pin
Furty21-Jul-03 11:02
Furty21-Jul-03 11:02 
GeneralFolderTreeView lists the zip-files on my desktop Pin
Terje Krång10-Jun-03 3:54
Terje Krång10-Jun-03 3:54 
GeneralRe: FolderTreeView lists the zip-files on my desktop Pin
eidylon29-Aug-03 7:35
eidylon29-Aug-03 7:35 
GeneralRe: FolderTreeView lists the zip-files on my desktop Pin
Furty29-Aug-03 12:01
Furty29-Aug-03 12:01 
GeneralHi Furty Pin
cuteo20009-May-03 13:36
cuteo20009-May-03 13:36 
GeneralRe: Hi Furty Pin
Jim Wiese (aka Spunk)8-Jul-03 13:09
Jim Wiese (aka Spunk)8-Jul-03 13:09 
GeneralRe: Hi Furty Pin
tunt5-Nov-03 16:47
tunt5-Nov-03 16:47 

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.