如何在树视图中显示所有目录? (C#)

时间:2018-08-07 14:52:51

标签: c#

嗨,这是我的代码,与此有关。 如何将节点添加到最后一个节点的前面? 我想创建自己的文件资源管理器。我发现很好的例子,但是它使用其他工具来做到这一点。我的问题只是treeview

public Form1()
    {
        InitializeComponent();
        treeView1.Nodes.Add("Explorer");
    }


    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();

        if (fbd.ShowDialog() == DialogResult.OK) {

            DirectoryInfo directoriyInfo = new DirectoryInfo(fbd.SelectedPath);

            try
            {

                DirectoryInfo[] directories = directoriyInfo.GetDirectories();

                foreach (DirectoryInfo directory in directories) {

                    TreeNode node = treeView1.Nodes[0].Nodes.Add(directory.Name);

                    foreach (FileInfo files in directory.GetFiles()) {

                        TreeNode nodes = treeView1.Nodes[0].Nodes[node.Index].Nodes.Add(files.Name);
                    }
                    SearchInDirectory(directory , node.Index);
                }
            }
            catch (Exception ex) {

                MessageBox.Show(ex.Message);
            }

        }

    }

    private void SearchInDirectory(DirectoryInfo directory , int theNode) {


        DirectoryInfo[] directories = directory.GetDirectories();

        foreach (DirectoryInfo searchDirectory in directories)
        {

            TreeNode node = treeView1.Nodes[0].Nodes[theNode].Nodes.Add(searchDirectory.Name);

            foreach (FileInfo files in searchDirectory.GetFiles())
            {

                TreeNode nodes = treeView1.Nodes[0].Nodes[theNode].Nodes[node.Index].Nodes.Add(files.Name);
            }
            SearchInDirectory(searchDirectory , node.Index);
        }
    }

在本节代码中。假设我们有100个方向。我应该在代码中添加100个Nodes [0] .Nodes [0] ....吗? :)

TreeNode nodes = treeView1.Nodes[0].Nodes[theNode].Nodes[node.Index].Nodes.Add(files.Name);

0 个答案:

没有答案