//get root directory
DirectoryInfo rootDir = new DirectoryInfo(Server.MapPath("~path"));
//create and add the root node to the tree view
TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
TreeView1.Nodes.Add(rootNode);
//begin recursively traversing the directory structure
TraverseTree(rootDir, rootNode);
private void TraverseTree(DirectoryInfo currentDir, TreeNode currentNode)
{
//loop through each sub-directory in the current one
foreach (DirectoryInfo dir in currentDir.GetDirectories())
{
//create node and add to the tree view
TreeNode node = new TreeNode(dir.Name, dir.FullName);
currentNode.ChildNodes.Add(node);
//recursively call same method to go down the next level of the tree
TraverseTree(dir, node);
}
//loop through each sub-directory in the current one
foreach (FileInfo file in currentDir.GetFiles())
{
//create node and add to the tree view
TreeNode node = new TreeNode(file.Name, file.FullName);
currentNode.ChildNodes.Add(node);
}
}