如何在treeview中显示文件夹下的所有文件

时间:2016-02-02 17:05:42

标签: c# winforms treeview

Iam尝试使用winforms获取treeview控件中的所有驱动器,文件夹,子文件夹和文件。我看过以下文章。

http://codehill.com/2013/06/list-drives-and-folders-in-a-treeview-using-c/

但这仅显示驱动器,文件夹和子文件夹,但不包含这些文件夹中包含的文件。

请任何人帮助和指导我如何在树视图中查看这些文件夹下的所有这些文件,提前感谢。

编辑:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetDrives
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            //get a list of the drives
            string[] drives = Environment.GetLogicalDrives();

            foreach (string drive in drives)
            {
                DriveInfo di = new DriveInfo(drive);
                int driveImage;

                switch (di.DriveType)    //set the drive's icon
                {
                    case DriveType.CDRom:
                        driveImage = 3;
                        break;
                    case DriveType.Network:
                        driveImage = 6;
                        break;
                    case DriveType.NoRootDirectory:
                        driveImage = 8;
                        break;
                    case DriveType.Unknown:
                        driveImage = 8;
                        break;
                    default:
                        driveImage = 2;
                        break;
                }

                TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
                node.Tag = drive;

                if (di.IsReady == true)
                    node.Nodes.Add("...");

                dirsTreeView.Nodes.Add(node);
            }

        }

        private void dirsTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
                {
                    e.Node.Nodes.Clear();

                    //get the list of sub direcotires
                    string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

                    foreach (string dir in dirs)
                    {
                        DirectoryInfo di = new DirectoryInfo(dir);
                        TreeNode node = new TreeNode(di.Name, 0, 1);

                        try
                        {
                            //keep the directory's full path in the tag for use later
                            node.Tag = dir;

                            //if the directory has sub directories add the place holder
                            if (di.GetDirectories().Count() > 0)
                                node.Nodes.Add(null, "...", 0, 0);

                            foreach (var file in di.GetFiles())
                            {
                                TreeNode n = new TreeNode(file.Name, 13, 13);
                                node.Nodes.Add(n);
                            }

                        }
                        catch (UnauthorizedAccessException)
                        {
                            //display a locked folder icon
                            node.ImageIndex = 12;
                            node.SelectedImageIndex = 12;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "DirectoryLister",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        finally
                        {
                            e.Node.Nodes.Add(node);
                        }
                    }
                }
            }
        }
    }
}

我现在已经在应用程序中更新了我的代码,并且只使用了一个树视图,但问题仍然存在。你可以在图像中看到,在我的C盘中我有一个文件名" course outline.html"和ab.txt没有在应用程序中显示,我需要看到。请参阅下面的iamge以轻松了解我的要求。 click on this image

2 个答案:

答案 0 :(得分:2)

更改try块中的代码(来自:List Drives and Folders in a TreeView Using C#),如下所示:

修改

添加了以下代码,因为根目录的文件被忽略了:

//add files of rootdirectory
DirectoryInfo rootDir = new DirectoryInfo(e.Node.Tag.ToString());
foreach (var file in rootDir.GetFiles())
{
    TreeNode n = new TreeNode(file.Name, 13, 13);
    e.Node.Nodes.Add(n);
}

全班:

private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
    if (e.Node.Nodes.Count > 0)
    {
        if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
        {
            e.Node.Nodes.Clear();

            //get the list of sub direcotires
            string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

            //add files of rootdirectory
            DirectoryInfo rootDir = new DirectoryInfo(e.Node.Tag.ToString());
            foreach (var file in rootDir.GetFiles())
            {
                TreeNode n = new TreeNode(file.Name, 13, 13);
                e.Node.Nodes.Add(n);
            }

            foreach (string dir in dirs)
            {
                DirectoryInfo di = new DirectoryInfo(dir);
                TreeNode node = new TreeNode(di.Name, 0, 1);

                try
                {
                    //keep the directory's full path in the tag for use later
                    node.Tag = dir;

                    //if the directory has sub directories add the place holder
                    if (di.GetDirectories().Count() > 0)
                        node.Nodes.Add(null, "...", 0, 0);

                    foreach (var file in di.GetFiles())
                    {
                        TreeNode n = new TreeNode(file.Name, 13, 13);
                        node.Nodes.Add(n);
                    }

                }
                catch (UnauthorizedAccessException)
                {
                    //display a locked folder icon
                    node.ImageIndex = 12;
                    node.SelectedImageIndex = 12;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "DirectoryLister",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    e.Node.Nodes.Add(node);
                }
            }
        }
    }
}

这看起来如下图:

enter image description here

答案 1 :(得分:1)

您可以使用enumerate files方法获取目录位置内每个文件的名称。

string sourceDirectory = @"C:\current";
var files = Directory.EnumerateFiles(sourceDirectory);

foreach (string file in files)
{
    // Do Something with file
}