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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.