Refer below sample code for your reference.
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="124254.aspx.cs" Inherits="_124254" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
html, body
{
margin: 0;
padding: 0;
height: 100%;
}
.modalBackground
{
background-color: Black;
filter: alpha(opacity=40);
opacity: 0.4;
}
.modalPopup
{
background-color: #FFFFFF;
width: 300px;
border: 3px solid #0DA9D0;
}
.modalPopup .header
{
background-color: #2FBDF1;
height: 30px;
color: White;
line-height: 30px;
text-align: center;
font-weight: bold;
}
.modalPopup .body
{
min-height: 50px;
line-height: 30px;
text-align: center;
padding: 5px;
}
.modalPopup .footer
{
padding: 3px;
}
.modalPopup .button
{
height: 23px;
color: White;
line-height: 23px;
text-align: center;
font-weight: bold;
cursor: pointer;
background-color: #9F9F9F;
border: 1px solid #5C5C5C;
}
.modalPopup td
{
text-align: left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<div>
<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>
<br />
<br />
</div>
<cc1:ModalPopupExtender ID="ModalPopupExtender1" BehaviorID="mpe" runat="server"
PopupControlID="pnlPopup" TargetControlID="btnAddDir" BackgroundCssClass="modalBackground"
CancelControlID="btnCancel">
</cc1:ModalPopupExtender>
<asp:Button ID="btnAddDir" Text="Add Directory" runat="server" />
<asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none;
width: 30%">
<div class="header">
Add Directories
</div>
<div class="body">
<br />
<asp:TextBox ID="txtDirName" runat="server" />
<br />
<br />
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddDirectory" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</div>
</asp:Panel>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo("C:\\TestFolder\\");
// output the directory into a node
TreeNode RootNode = OutputDirectory(RootDir, null);
// add the output to the tree
MyTree.Nodes.Add(new TreeNode("C:", "C:"));
MyTree.Nodes[0].ChildNodes.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
try
{
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);
}
}
catch
{
}
if (parentNode == null)
{
return DirNode;
}
else
{
parentNode.ChildNodes.Add(DirNode);
return parentNode;
}
}
protected void AddDirectory(object sender, EventArgs e)
{
if (MyTree.SelectedNode != null)
{
if (!string.IsNullOrEmpty(this.txtDirName.Text))
{
string firstFolder = MyTree.SelectedNode.ValuePath;
string subFolder = Path.Combine(firstFolder, txtDirName.Text.Trim());
if (!Directory.Exists(subFolder))
{
Directory.CreateDirectory(subFolder);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Folder already exists.');", true);
}
MyTree.SelectedNode.ChildNodes.Add(new TreeNode(this.txtDirName.Text.Trim(), this.txtDirName.Text.Trim()));
}
}
else
{
this.ClientScript.RegisterStartupScript(typeof(Page), "select", "alert('Please select at least one parent directory')", true);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim RootDir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("C:\TestFolder\")
Dim RootNode As TreeNode = OutputDirectory(RootDir, Nothing)
MyTree.Nodes.Add(New TreeNode("C:", "C:"))
MyTree.Nodes(0).ChildNodes.Add(RootNode)
End If
End Sub
Private Function OutputDirectory(ByVal directory As System.IO.DirectoryInfo, ByVal parentNode As TreeNode) As TreeNode
If directory Is Nothing Then Return Nothing
Dim DirNode As TreeNode = New TreeNode(directory.Name)
Try
Dim SubDirectories As System.IO.DirectoryInfo() = directory.GetDirectories()
For DirectoryCount As Integer = 0 To SubDirectories.Length - 1
OutputDirectory(SubDirectories(DirectoryCount), DirNode)
Next
Catch
End Try
If parentNode Is Nothing Then
Return DirNode
Else
parentNode.ChildNodes.Add(DirNode)
Return parentNode
End If
End Function
Protected Sub AddDirectory(ByVal sender As Object, ByVal e As EventArgs)
If MyTree.SelectedNode IsNot Nothing Then
If Not String.IsNullOrEmpty(Me.txtDirName.Text) Then
Dim firstFolder As String = MyTree.SelectedNode.ValuePath
Dim subFolder As String = Path.Combine(firstFolder, txtDirName.Text.Trim())
If Not Directory.Exists(subFolder) Then
Directory.CreateDirectory(subFolder)
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Folder already exists.');", True)
End If
MyTree.SelectedNode.ChildNodes.Add(New TreeNode(Me.txtDirName.Text.Trim(), Me.txtDirName.Text.Trim()))
End If
Else
Me.ClientScript.RegisterStartupScript(GetType(Page), "select", "alert('Please select at least one parent directory')", True)
End If
End Sub
Screenshot