ASP.Net TreeView will display server's folder structure. It is not possible to select any folder and display it contents.
As in ASP.Net there's no control to select a folder and extract its path.
Following is what I have created for you.
HTML
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
Namespaces
using System.IO;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DirectoryInfo rootDir = new DirectoryInfo("D:\\Mudassar\\Vehicles\\");
this.PopulateTreeView(rootDir, null);
}
}
private void PopulateTreeView(DirectoryInfo directory, TreeNode treeNode)
{
foreach (DirectoryInfo dir in directory.GetDirectories())
{
TreeNode child = new TreeNode
{
Text = dir.Name,
Value = dir.FullName
};
if (treeNode == null)
{
TreeView1.Nodes.Add(child);
}
else
{
treeNode.ChildNodes.Add(child);
}
foreach (FileInfo f in dir.GetFiles())
{
TreeNode file = new TreeNode
{
Text = f.Name,
Value = f.FullName
};
child.ChildNodes.Add(file);
}
PopulateTreeView(dir, child);
}
}
Screenshot
