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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With