4

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?

7
  • Your Substring hurts my brain. Did you know that FileInfo has an Extension property? Commented Apr 9, 2015 at 20:37
  • @Biscuits No, I didn't know that Commented Apr 9, 2015 at 20:37
  • Ok. Nevertheless, I would opt to use the GetFiles overload on DirectoryInfo that allows you to specify the search pattern upfront. Commented Apr 9, 2015 at 20:42
  • @Biscuits Can you give me an example in code? Commented Apr 9, 2015 at 20:46
  • 1
    foreach (FileInfo file in subFolder.GetFiles("*.bch")) Commented Apr 9, 2015 at 20:49

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

I just tried your code, I am still just getting the folders, no files. Am I missing something?
Then I think the problem is that the extension is not really .bch. Try following: windows.microsoft.com/en-us/windows/…
No it really is the file extension. I am going to post a pic of the files for you.
What happens when you put a breakpoint on the line "var tn =..."? Is it ever hit?
Aren't those files marked as hidden? (However GetFiles() should return even hidden files)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.