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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » TreeViewScreenshots 
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.Addpath );
        PopulateTreeView(path, directoryTreeView.Nodes] );
      }  

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

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

                  TreeNode myNode = new TreeNodesubstringDirectory );

                  parentNode.Nodes.AddmyNode );

                  PopulateTreeViewdirectory, 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(1277);
         this.directoryTreeView.Name = "directoryTreeView";
         this.directoryTreeView.Size = new System.Drawing.Size(284265);
         this.directoryTreeView.TabIndex = 0;
         // 
         // TreeViewDirectoryStructureForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(304354);
         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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.