Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a TreeView with a string directory

How to I populate a TreeView with a directory as a string. I am using the FolderBrowserDialog to select a folder and the SelectedPath property to get the string path (i.e. C:\Users\Admin).


Also, could I view files like this?

like image 338
Mohit Deshpande Avatar asked Dec 28 '09 17:12

Mohit Deshpande


1 Answers

private void button1_Click(object sender, EventArgs e)
{
    FolderBrowserDialog dialog = new FolderBrowserDialog();
    if (dialog.ShowDialog() != DialogResult.OK) { return; }

    this.treeView1.Nodes.Add(TraverseDirectory(dialog.SelectedPath));

}


private TreeNode TraverseDirectory(string path)
{
    TreeNode result = new TreeNode(path);
    foreach (var subdirectory in Directory.GetDirectories(path))
    {
        result.Nodes.Add(TraverseDirectory(subdirectory));
    }

    return result;
}
like image 198
BFree Avatar answered Sep 17 '22 11:09

BFree