Search code examples
c#asp.nettreeviewtreenodedirectoryinfo

Populating a treeview with files in ASP.NET C#


I have a treeview that I am trying to populate with folders and files. The treeview is populating the folders just fine but not the files. Here is my code:

protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                PopulateTree();
            }              

        }

private void PopulateTree()
        {
            //Populate the tree based on the subfolders of the specified VirtualImageRoot
            var rootFolder = new DirectoryInfo(VirtualImageRoot);
            var root = AddNodeAndDescendents(rootFolder, null);

            //Add the root to the TreeView
            TreeView1.Nodes.Add(root);
        }

private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
        {
            //Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
            string virtualFolderPath;

            if (parentNode == null)
            {
                virtualFolderPath = VirtualImageRoot;
            }
            else
            {
                virtualFolderPath = parentNode.Value + folder.Name + "/";
            }

            var node = new TreeNode(folder.Name, virtualFolderPath);

            //Recurse through this folder's subfolders
            var subFolders = folder.GetDirectories();

            foreach (DirectoryInfo subFolder in subFolders)
            {
                var child = AddNodeAndDescendents(subFolder, node);

                foreach (FileInfo file in subFolder.GetFiles())
                {

                    var index = file.FullName.LastIndexOf(@"\", StringComparison.Ordinal);
                    var strname = file.FullName.Substring(index + 1);
                    var name = strname.Split('.');

                    var tn = new TreeNode();
                    if (name.Length > 1 && name[1].ToLower() == "bch")
                    {
                        tn = new TreeNode(name[0], file.FullName);
                    }
                    else
                    {
                        tn = new TreeNode(name[0], file.FullName);
                    }
                    child.ChildNodes.Add(tn);
                }
                node.ChildNodes.Add(child);

            }
            //Return the new TreeNode
            return node;
        }

Here is what my tree looks like:

My Tree View: without files

Here is a picture of the files in the folder:

My files

I am trying just to show the files with the type .bch, along with the folders in my treeview. Can someone please tell me what I am doing wrong?


Solution

  • The problem was that your code didn't take into account the first level of the folder hierarchy:

    private void PopulateTree()
    {
        var rootFolder = new DirectoryInfo(@"C:\inetpub\wwwroot\yourwebproject");
        var root = AddNodeAndDescendents(rootFolder);
        TreeView1.Nodes.Add(root);
    }
    
    private TreeNode AddNodeAndDescendents(DirectoryInfo folder)
    {        
        var node = new TreeNode(folder.Name, folder.Name);
    
        var subFolders = folder.GetDirectories();
    
        foreach (DirectoryInfo subFolder in subFolders)
        {
            var child = AddNodeAndDescendents(subFolder);
            node.ChildNodes.Add(child);
        }
    
        foreach (FileInfo file in folder.GetFiles("*.bch"))
        {
            var tn = new TreeNode(file.Name, file.FullName);
            node.ChildNodes.Add(tn);
        }
        return node;
    }