SIGN UP MEMBER LOGIN:    
ARTICLE

Display Directory Structure Using TreeView Control in ASP.NET

Posted by Ankit Nandekar Articles | Windows Forms C# February 24, 2011
The sample code in this article demonstrate how to display a directory structure using a TreeView Control in ASP.NET
Reader Level:

The following code demonstrates how to display a directory structure using a TreeView Control in ASP.NET.


Step1: Drag Treeview Control from the ASP.NET toolbox.



<asp:TreeView Id="MyTree"

                      PathSeparator = "|"

                      ExpandDepth="0"

                      runat="server" ImageSet="Arrows"

        AutoGenerateDataBindings="False">

            <SelectedNodeStyle Font-Underline="True"
                HorizontalPadding="0px" VerticalPadding="0px" ForeColor="#5555DD"></SelectedNodeStyle>

            <NodeStyle VerticalPadding="0px" Font-Names="Tahoma" Font-Size="10pt"

                HorizontalPadding="5px" ForeColor="#000000" NodeSpacing="0px"></NodeStyle>
            <ParentNodeStyle Font-Bold="False" />

            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD"></HoverNodeStyle>

         </asp:TreeView>


Step 2:

Write the following code in .cs file .........


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Media;
using System.Drawing;
using System.Drawing.Imaging;

public partial class CreateFolder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (Page.IsPostBack == false)
        {
             System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));
             // output the directory into a node
            TreeNode RootNode = OutputDirectory(RootDir, null);

            // add the output to the tree

             MyTree.Nodes.Add(RootNode);

        }

    }

   

    TreeNode OutputDirectory(System.IO.DirectoryInfo directory, TreeNode parentNode)
    {
        // validate param
        if (directory == null) return null; 

        // create a node for this directory

        TreeNode DirNode = new TreeNode(directory.Name);

        // get subdirectories of the current directory

        System.IO.DirectoryInfo[] SubDirectories = directory.GetDirectories();

       // OutputDirectory(SubDirectories[0], "Directories");

        // output each subdirectory

        for (int DirectoryCount = 0; DirectoryCount < SubDirectories.Length; DirectoryCount++)

        {

            OutputDirectory(SubDirectories[DirectoryCount], DirNode);

        }

        // output the current directories file

        System.IO.FileInfo[] Files = directory.GetFiles();

        for (int FileCount = 0; FileCount < Files.Length; FileCount++)

        {

            DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name));

        }        // if the parent node is null, return this node

        // otherwise add this node to the parent and return the parent

        if (parentNode == null)

        {

            return DirNode;

        }

        else

        {

            parentNode.ChildNodes.Add(DirNode);

            return parentNode;
        }
    }

  }


Run your website.....



Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor