[C#]
void Page_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//You can also set local path too. But ASP.NET account should have read
//permission for the directory
string path = HttpContext.Current.Request.PhysicalApplicationPath;
if (!Directory.Exists(path))
{
throw new InvalidDataException("Could not find the directory in the specified path.");
}
DirectoryInfo rootDirectory = new DirectoryInfo(path);
Node rootNode = new Node() { Text = rootDirectory.Name, Value = rootDirectory.FullName,
ImageUrl = "img/folder.png", Expanded = true };
this.Tree1.Nodes.Add(rootNode);
this.PopulateChildNodes(rootNode);
}
}
private void PopulateChildNodes(Node parentNode)
{
DirectoryInfo parentDirectory = new DirectoryInfo(parentNode.Value);
//Populate directories
//If you dont want to sort the nodes, use only 'parentDirectory.GetDirectories()'
foreach (DirectoryInfo dir in parentDirectory.GetDirectories().OrderBy(d => d.Name))
{
parentNode.ChildNodes.Add(new Node() { Text = dir.Name, Value = dir.FullName,
ExpandMode = NodeExpandMode.ServerSideCallback, ImageUrl = "img/folder.png" });
}
//Populate files
foreach (FileInfo dir in parentDirectory.GetFiles().OrderBy(f => f.Name))
{
parentNode.ChildNodes.Add(new Node() { Text = dir.Name, ImageUrl = "img/document.png" });
}
}
protected void Tree1_TreeNodeExpanded(object sender, NodeEventArgs e)
{
this.PopulateChildNodes(e.Node);
}
[VB]
Private Sub Page_load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
'You can also set local path too. But ASP.NET account should have read
'permission for the directory
Dim path As String = HttpContext.Current.Request.PhysicalApplicationPath
If Not Directory.Exists(path) Then
Throw New InvalidDataException("Could not find the directory in the specified path.")
End If
Dim rootDirectory As New DirectoryInfo(path)
Dim rootNode As New Node() With {.Text = rootDirectory.Name, .Value = rootDirectory.FullName,
.ImageUrl = "img/folder.png", .Expanded = True}
Me.Tree1.Nodes.Add(rootNode)
Me.PopulateChildNodes(rootNode)
End If
End Sub
Private Sub PopulateChildNodes(ByVal parentNode As Node)
Dim parentDirectory As New DirectoryInfo(parentNode.Value)
'Populate directories
'If you dont want to sort the nodes, use only 'parentDirectory.GetDirectories()'
For Each dir As DirectoryInfo In parentDirectory.GetDirectories().OrderBy(Function(d) d.Name)
parentNode.ChildNodes.Add(New Node() With {.Text = dir.Name, .Value = dir.FullName,
.ExpandMode = NodeExpandMode.ServerSideCallback, .ImageUrl = "img/folder.png"})
Next dir
'Populate files
For Each dir As FileInfo In parentDirectory.GetFiles().OrderBy(Function(f) f.Name)
parentNode.ChildNodes.Add(New Node() With {.Text = dir.Name, .ImageUrl = "img/document.png"})
Next dir
End Sub
Protected Sub Tree1_TreeNodeExpanded(ByVal sender As Object, ByVal e As NodeEventArgs)
Me.PopulateChildNodes(e.Node)
End Sub