static void

DirTreeView (for Windows.Forms)

A simple user control that uses DirTreeViewHelper. Link an ImageList for the folder/drive images. The NodeClicked event uses DirectoryClickedEventArgs.

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
 
namespace Library.Forms
{
    [ToolboxBitmap(typeof(TreeView))]
    [DefaultEvent("NodeClicked"), DefaultProperty("CurrentDirectory"), DefaultBindingProperty("CurrentDirectory"), Description("A treeview for directories")]
    [Designer("System.Windows.Forms.Design.TreeViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    public partial class DirTreeView : UserControl
    {
        public event EventHandler<DirectoryClickedEventArgs> NodeClicked;
        private readonly DirTreeViewHelper dtv;
        private readonly DirectoryInfo CDrive;
        private DirectoryInfo _currentDirectory;
 
        public DirTreeView()
        {
            InitializeComponent();
 
            dtv = new DirTreeViewHelper(treeView1);
            CDrive = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal)).Root;
            InitTreeViewEvents();
        }
 
        private void InitTreeViewEvents()
        {
            treeView1.BeforeExpand += delegate(object sender, TreeViewCancelEventArgs e)
            {
                dtv.ExpandNode(e.Node);
            };
            treeView1.NodeMouseClick += delegate(object sender, TreeNodeMouseClickEventArgs e)
            {
                _currentDirectory = dtv.GetDirectory(e.Node);
                EventHandler<DirectoryClickedEventArgs> handler = NodeClicked;
                if (handler != null) handler(this, new DirectoryClickedEventArgs(_currentDirectory));
            };
        }
        private ImageList _imageList = null;
 
        [Browsable(true), Description("The imagelist used by the treenodes"), Category("Behavior")]
        public ImageList ImageList
        {
            get { return _imageList; }
            set
            {
                _imageList = value;
                treeView1.ImageList = _imageList;
            }
        }
 
        [Browsable(true), Description("The current directory"), Category("Behavior")]
        public string CurrentDirectory
        {
            get
            {
                return _currentDirectory == null ? string.Empty : _currentDirectory.FullName;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    value = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                DirectoryInfo dir = new DirectoryInfo(value);
                if (!dir.Exists) throw new ArgumentException("Directory does not exist");
                CurrentDirectoryInfo = dir;
            }
        }
        [Browsable(false), Category("Behavior"), Description("The current directory"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DirectoryInfo CurrentDirectoryInfo
        {
            get { return _currentDirectory; }
            set
            {
                if (value == null) value = CDrive;
                _currentDirectory = value;
                dtv.PopulateTreeView(value);
            }
        }
 
        #region Designer Code
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Component Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            //
            // treeView1
            //
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(150, 150);
            this.treeView1.TabIndex = 0;
            //
            // DirTreeView
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.treeView1);
            this.Name = "DirTreeView";
            this.ResumeLayout(false);
 
        }
        #endregion
 
        private System.Windows.Forms.TreeView treeView1;
        #endregion
    }
}