How to display directories in a TreeView? [duplica

2019-04-07 15:42发布

This question already has an answer here:

Below is my code

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");

private void Form1_Load(object sender, EventArgs e)
{
    if (Directory.Exists("FileExplorer"))
    {
        try
        {
            DirectoryInfo[] directories = directoryInfo.GetDirectories();

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (file.Exists)
                {
                    TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
                }
            }

            if (directories.Length > 0)
            {
                foreach (DirectoryInfo directory in directories)
                {
                    TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
                    node.ImageIndex = node.SelectedImageIndex = 0;
                    foreach (FileInfo file in directory.GetFiles())
                    {
                        if (file.Exists)
                        {
                            TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

When I run I just get a blank treeview form? Unable to figure out what is the error?

Btw this my first post in Stack Overflow.

标签: c# treeview
4条回答
劳资没心,怎么记你
2楼-- · 2019-04-07 15:57

Try this: (note make sure your directoryInfo location contains some folders)

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");

private void Form1_Load(object sender, EventArgs e)
{
    if (directoryInfo.Exists)
    {
        try
        {
            treeView.Nodes.Add(directoryInfo.Name);
            DirectoryInfo[] directories = directoryInfo.GetDirectories();

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (file.Exists)
                {
                    TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
                }
            }


            if (directories.Length > 0)
            {
                foreach (DirectoryInfo directory in directories)
                {
                    TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
                    node.ImageIndex = node.SelectedImageIndex = 0;
                    foreach (FileInfo file in directory.GetFiles())
                    {
                        if (file.Exists)
                        {
                            TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
查看更多
唯我独甜
3楼-- · 2019-04-07 16:12

This should solve your problem, I tried on WinForm though:

public Form1()
    {
        InitializeComponent();

        DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\hikuma\Documents\IR");
        if (directoryInfo.Exists)
        {
            treeView1.AfterSelect += treeView1_AfterSelect;
            BuildTree(directoryInfo, treeView1.Nodes);
        }
    }

    private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
    {
        TreeNode curNode = addInMe.Add(directoryInfo.Name);

        foreach (FileInfo file in directoryInfo.GetFiles())
        {
            curNode.Nodes.Add(file.FullName, file.Name);
        }
        foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
        {
            BuildTree(subdir, curNode.Nodes);
        }
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        if(e.Node.Name.EndsWith("txt"))
        {
            this.richTextBox1.Clear();
            StreamReader reader = new StreamReader(e.Node.Name);
            this.richTextBox1.Text = reader.ReadToEnd();
            reader.Close();
        }
    }

It is a simple example of how you can open file in rich text box, it can be improved a lot :). You might want to mark as answer or vote up if it helped :) !!

查看更多
做自己的国王
4楼-- · 2019-04-07 16:12

Try the following:

private void Form1_Load(object sender, EventArgs e)
    {
        if (directoryInfo.Exists)
        {
            try
            {
                treeView.Nodes.Add(LoadDirectory(directoryInfo));                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    private TreeNode LoadDirectory(DirectoryInfo di)
    {
        if (!di.Exists)
            return null;

        TreeNode output = new TreeNode(di.Name, 0, 0);

        foreach (var subDir in di.GetDirectories())
        {
            try
            {
                output.Nodes.Add(LoadDirectory(subDir));
            }
            catch (IOException ex)
            {
                //handle error
            }
            catch { }
        }

        foreach (var file in di.GetFiles())
        {
            if (file.Exists)
            {
                output.Nodes.Add(file.Name);
            }
        }

        return output;
    }
}

It's better to split out the directory parsing into a recursive method so that you can go all the way down the tree.

This WILL block the UI until it's completely loaded - but fixing that is beyond the scope of this answer...

:)

查看更多
▲ chillily
5楼-- · 2019-04-07 16:18

DirectoryInfo.Exists("FileExplorer") will check for "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\debug\FileExplorer", not "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer", when you are running in debug mode.

查看更多
登录 后发表回答