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

Andrew BhaurPosted Mar 7, 2024, 1:12 AM
With HTML, CSS, and JavaScript, you can create stunning websites and web apps with the help of the free web framework ASP.NET. Having worked with ASP.NET for 12 years, you have a solid background in web development. You might think about studying about contemporary front-end frameworks like React or Angular, as well as back-end technologies like Node.js or Python, to improve your abilities and improve your employment possibilities.