Recursively load Directory info into TreeView : TreeView « GUI Windows Form « C# / C Sharp






Recursively load Directory info into TreeView

Recursively load Directory info into TreeView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

public class Form1 : Form
{
      private System.Windows.Forms.TreeView directoryTreeView;
     
      string substringDirectory;
       
      public Form1() {
        InitializeComponent();
        directoryTreeView.Nodes.Clear();
        
        String path = "c:\\Temp";

        directoryTreeView.Nodes.Add( path );
        PopulateTreeView(path, directoryTreeView.Nodes[ 0 ] );
      }  

      public void PopulateTreeView(string directoryValue, TreeNode parentNode )
      {
          string[] directoryArray = 
           Directory.GetDirectories( directoryValue );

          try
          {
             if ( directoryArray.Length != 0 )
             {
                foreach ( string directory in directoryArray )
                {
                  substringDirectory = directory.Substring(
                  directory.LastIndexOf( '\\' ) + 1,
                  directory.Length - directory.LastIndexOf( '\\' ) - 1 );

                  TreeNode myNode = new TreeNode( substringDirectory );

                  parentNode.Nodes.Add( myNode );

                  PopulateTreeView( directory, myNode );
               }
             }
          } catch ( UnauthorizedAccessException ) {
            parentNode.Nodes.Add( "Access denied" );
          } // end catch
      }

      private void InitializeComponent()
      {
         this.directoryTreeView = new System.Windows.Forms.TreeView();
         this.SuspendLayout();
         // 
         // directoryTreeView
         // 
         this.directoryTreeView.Location = new System.Drawing.Point(12, 77);
         this.directoryTreeView.Name = "directoryTreeView";
         this.directoryTreeView.Size = new System.Drawing.Size(284, 265);
         this.directoryTreeView.TabIndex = 0;
         // 
         // TreeViewDirectoryStructureForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(304, 354);
         this.Controls.Add(this.directoryTreeView);
         this.Name = "TreeViewDirectoryStructureForm";
         this.Text = "TreeViewDirectoryStructure";
         this.ResumeLayout(false);
         this.PerformLayout();

      }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}


           
       








Related examples in the same category

1.Drag and drop Tree NodeDrag and drop Tree Node
2.Get Selected Node Full PathGet Selected Node Full Path
3.Custom TreeView
4.TreeView ExampleTreeView Example
5.TreeView Drag And DropTreeView Drag And Drop
6.TreeView Data BindingTreeView Data Binding
7.Read an XML Document and display the file as a TreeRead an XML Document and display the file as a Tree
8.TreeView DemoTreeView Demo
9.Directory Tree HostDirectory Tree Host
10.Subclass TreeView
11.Add Nodes to TreeView
12.TreeView ImageIndex