Populate a TreeView with a string directory

后端 未结 3 1101
一向
一向 2021-01-18 06:14

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

3条回答
  •  青春惊慌失措
    2021-01-18 07:01

    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;
    }
    

提交回复
热议问题