Display Directory Structure Using TreeView Control in ASP.NET


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.....




Similar Articles