如何在TreeView中显示目录?

时间:2013-05-01 08:32:47

标签: c# treeview

以下是我的代码

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

当我跑步时,我只得到一张空白的树形图?无法弄清楚错误是什么?

这是我在Stack Overflow中的第一篇文章。

4 个答案:

答案 0 :(得分:8)

这应该可以解决你的问题,我试过WinForm:

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

这是一个如何在富文本框中打开文件的简单示例,它可以得到很多改进:)。 如果它有帮助,您可能想要标记为答案或投票:) !!

答案 1 :(得分:0)

试试这个:(注意确保你的directoryInfo位置包含一些文件夹)

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

答案 2 :(得分:0)

DirectoryInfo.Exists(“FileExplorer”)将检查“C:\ Users \ Shahul \ Documents \ Visual Studio 2010 \ Projects \ TreeView \ TreeView \ bin \ debug \ FileExplorer”,而不是“C:\ Users \ Shahul \”在调试模式下运行时,Documents \ Visual Studio 2010 \ Projects \ TreeView \ TreeView \ bin \ FileExplorer“。

答案 3 :(得分:0)

尝试以下方法:

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

最好将目录解析拆分为递归方法,以便您可以一直走到树下。

这会阻止用户界面,直到它被完全加载 - 但修复它超出了这个答案的范围......

:)