shahi
shahi

Reputation: 1

How to show sub folders present in a selected Folder?

I have one text box which accepts the path of selected folder. And i need to display all sub folders present in that folder in a TreeView.

How can i do this?

Upvotes: 0

Views: 1411

Answers (3)

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26747

I have done this in the past using VB.net (I will convert it for you shortly) All you need is a recursive function

YourTreeViewControl.Nodes.Add("C:\Temp")
Recursive(@"C:\Temp", Tree.Nodes(0))

    void Recursive(string d, TreeNode nodePar)
    {
        DirectoryInfo dir = new DirectoryInfo(d);
        foreach (var item in dir.GetDirectories()) {
            Recursive(item.FullName, nodePar.Nodes.Add(item.Name));
        }
    }

Upvotes: 0

doneyjm
doneyjm

Reputation: 145

Following code will help u.

           DirectoryInfo parentInfo = new DirectoryInfo(@"path");
           DirectoryInfo[] childInfo= parentInfo.GetDirectories();
           treeView1.Nodes.Add(parentInfo.Name);
            foreach(DirectoryInfo di in childInfo)
            {
                treeView1.Nodes[0].Nodes.Add(di.Name);
            }

Upvotes: 0

Asif
Asif

Reputation: 2677

string Path = @"C:\Temp Folder\";
string[] folders = System.IO.Directory.GetDirectories(Path, "*", System.IO.SearchOption.TopDirectoryOnly); 

       TreeNode treeNode = new TreeNode(Path);          
        TreeNode subNode;
        for (int i = 0; i < folders.Length; i++)
        {
            subNode = new TreeNode(folders[i].ToString());
            treeNode.Nodes.Add(subNode);            
        }
        treeView1.Nodes.Add(treeNode);  

Upvotes: 2

Related Questions