Click here to Skip to main content
15,887,376 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 391.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

 
Questionhow can I write right to left treeview? Pin
shr27-Oct-02 21:30
shr27-Oct-02 21:30 
AnswerRe: how can I write right to left treeview? Pin
Srinivasa Raghavan28-Oct-02 0:38
Srinivasa Raghavan28-Oct-02 0:38 
QuestionWhy not BrowseForFolder? Pin
Heath Stewart26-Oct-02 19:11
protectorHeath Stewart26-Oct-02 19:11 
AnswerRe: Why not BrowseForFolder? Pin
Furty27-Oct-02 12:17
Furty27-Oct-02 12:17 
QuestionInterop.Shell32.dll...where did it come from? Pin
Jon Rista18-Oct-02 11:41
Jon Rista18-Oct-02 11:41 
AnswerRe: Interop.Shell32.dll...where did it come from? Pin
Furty19-Oct-02 11:58
Furty19-Oct-02 11:58 
GeneralRe: Interop.Shell32.dll...where did it come from? Pin
Jon Rista20-Oct-02 12:04
Jon Rista20-Oct-02 12:04 
GeneralRe: Interop.Shell32.dll...where did it come from? Pin
Furty20-Oct-02 12:54
Furty20-Oct-02 12:54 
Jon Rista wrote:
I'm working on an interop library of my own for shell32.dll, comctl32.dll, and uxtheme.dll, which will provide access to the system imagelist

The next revision has been finished, Chris should be updating the article over the next couple of days. V1.1 now properly starts at the Desktop namespace, showing My Computer, Network Neighborhood, Recycle Bin etc. I have also added a startup folder driller, but the imagelist works just the same as before. I had thought to write code to manage the icons better, but I guess I was just too lazy to save the 1KB here and there Smile | :)
If you are willing to share some of your code, perhaps you can help make this control better Wink | ;)
GeneralRe: Interop.Shell32.dll...where did it come from? Pin
Jon Rista20-Oct-02 19:42
Jon Rista20-Oct-02 19:42 
GeneralRe: Interop.Shell32.dll...where did it come from? Pin
Furty20-Oct-02 22:59
Furty20-Oct-02 22:59 
Generalbut it doesnot display very well in 98 Pin
daemonftp11-Oct-02 1:10
daemonftp11-Oct-02 1:10 
GeneralAdditional missing Namespaces Pin
Stephane Rodriguez.2-Oct-02 21:27
Stephane Rodriguez.2-Oct-02 21:27 
GeneralRe: Additional missing Namespaces Pin
Laurent Kempé2-Oct-02 22:43
Laurent Kempé2-Oct-02 22:43 
GeneralRe: Additional missing Namespaces Pin
Furty3-Oct-02 0:15
Furty3-Oct-02 0:15 
GeneralRe: Additional missing Namespaces Pin
KHK5-Mar-03 12:15
KHK5-Mar-03 12:15 

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.