How to display Files and folders in a hierarchical structure using ASP.NET Treeview control

ASP.Net Treeview control is a server control used to design data in a hierarchal structure. Here we are using Treeview control to display folders and files in a tree structure. Normally we use recursive function to accomplish this task. TreeView structure in ASP.NET helps you achieve this in few lines of code.

If you wanted to display Files and Folders in hierarchical structure the best control available in ASP.NET is TreeView Control. Here we will see a simple example of this control.

Treeview control is a navigation control in ASP.NET and it displays data in the form of a tree structure. In Treeview control we can add hierarchical data through built in wizard.

In below example we are using hard coded path which you can change it to dynamic if your project demands that.

We will see step by step implementation of Treeview below,
First you have to drag and drop three controls, one tree view control and two button control to expand and collapse the treeview.

How to display Files and folders in a hierarchical structure using ASP.NET Treeview control
You may use Treeview.ExpandAll() method to expand all the nodes of your tree view, similarly to collapse the treeview you may use TreeView.CollapseAll() method.

Since we have to loop through the Directory You have to add the System.IO namespace.

In below method what I have done is, first loop through the folders to find the parent node and then through the files inside the folders to find the child node.

.aspx page to design the Treeview page is simple and will looks like below,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="treeview.aspx.cs"
Inherits="Practice2010.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">

<asp:Button ID="btnExpand" runat="server" onclick="Button1_Click"
Text="Expand Nodes" />
<asp:Button ID="BtnCollapse" runat="server" onclick="BtnCollapse_Click"
Text="Collapse Nodes" />

<asp:TreeView ID="tvFolderView" runat="server" ExpandDepth="0"


Below is the complete code to create the ASP.NET Treeview control:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Practice2010
public partial class WebForm1 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

private void CreateTreeView()
DirectoryInfo parentDir = new DirectoryInfo("D:\\Song");
TreeNode parentNode = new TreeNode(parentDir.Name, parentDir.FullName);
CreateTree(parentDir, parentNode);
private void CreateTree(DirectoryInfo RootDir, TreeNode MyNode)
foreach (DirectoryInfo dir in RootDir.GetDirectories())
TreeNode objNode = new TreeNode(dir.Name, dir.FullName);
CreateTree(dir, objNode);
foreach (FileInfo File in RootDir.GetFiles())
TreeNode Filenode = new TreeNode(File.Name, File.FullName);

protected void Button1_Click(object sender, EventArgs e)

protected void tvFolderView_SelectedNodeChanged(object sender, EventArgs e)


protected void BtnCollapse_Click(object sender, EventArgs e)

Final output will looks like below,

How to display Files and folders in a hierarchical structure using ASP.NET Treeview control

You are always welcome to ask questions if you have any to me below.


Author: Nitin Patil15 Feb 2013 Member Level: Bronze   Points : 0

How can we drag & drop folder from one folder to another in using treeview

Plz Its Urgent

Guest Author: devendra28 May 2013

Thank you, great

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: