Populate TreeView with a String Directory

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


Also, is it possible to view files like this?

+7
c # winforms treeview
source share
3 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; } 
+10
source share

Add node directory to treeview. Set the node name to the full path and the text to the directory name.

Recursively add nodes to treeview. Use the System.IO collections of DirectoryInfo and FileInfo to retrieve files and directories in each DirectoryInfo object. make the final condition of your recursive function in the case when there are no child directories.

0
source share

You can use controls, such as FolderView and FileView, from Shell MegaPack . They can be placed in your own forms, rather than appearing modal dialog.

0
source share

All Articles