7

下面是我的代码

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

4 回答 4

10

这应该可以解决您的问题,但我在 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();
        }
    }

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

于 2013-05-01T09:18:25.163 回答
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);
        }
    }
}
于 2013-05-01T09:05:52.627 回答
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;
    }
}

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

这将阻止 UI 直到它完全加载 - 但修复它超出了这个答案的范围......

:)

于 2013-05-01T09:19:39.657 回答
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”,当您在调试模式下运行时。

于 2013-05-01T09:12:16.950 回答