Treeview与不同的文件夹类

时间:2016-09-15 05:20:52

标签: c# wpf treeview getdirectories

我试图将树视图与混合类的文件夹结合在一起。根文件夹是一个类,子文件夹是另一个,如下所示:

Root Folder #1  <-- DirectoryItem class
--Sub Folder    <-- SubDirectoryItem class
----file        <-- FileItem class
Root Folder #2  <-- DirectoryItem class
--Sub Folder    <-- SubDirectoryItem class
----file        <-- FileItem class

我一直试图结合两种不同的递归方法,因此子文件夹被检索为&#34; SubDirectoryItems&#34;而不是&#34; DirectoryItem&#34;由于缺乏澄清,我会饶恕代码尝试,但我想知道是否有更好的方法?

    class ItemProvider
{
    public List<Item> GetItems(string path, SearchOption searchOption)
    {
        var items = new List<Item>();
        var dirInfo = new DirectoryInfo(path);

        foreach (var directory in dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly))
        {
            var item = new DirectoryItem
            {
                Name = directory.Name,
                Path = directory.FullName,
                Items = GetItems(directory.FullName, SearchOption.AllDirectories)
            };

            items.Add(item);
        }

        foreach (var subdirectory in dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly))
        {
            var item = new SubDirectoryItem()
            {
                Name = subdirectory.Name,
                Path = subdirectory.FullName,
                Items = GetItems(subdirectory.FullName, SearchOption.AllDirectories)
            };

            items.Add(item);
        }

        foreach (var file in dirInfo.GetFiles())
        {
            var item = new FileItem
            {
                Name = file.Name,
                Path = file.FullName
            };

            items.Add(item);
        }       

        return items;
    }

}

}

1 个答案:

答案 0 :(得分:0)

我能够弄清楚。我创建了第一个列表TopDirectoryOnly然后通过AllDirectories的第二个列表拉入其余项目。这是代码,以防它可以帮助任何其他初学者(像我一样):

class ItemProvider
{
    public List<Item> GetItems(string path, SearchOption searchOption)
    {
        var items = new List<Item>();
        var dirInfo = new DirectoryInfo(path);

        foreach (var directory in dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly))
        {
            var item = new DirectoryItem
            {
                Name = directory.Name,
                Path = directory.FullName,
                Items = GetSubItems(directory.FullName, SearchOption.AllDirectories)
            };

            items.Add(item);
        }

        return items;
    }

    public List<Item> GetSubItems(string path, SearchOption searchOption)
    {
        var items = new List<Item>();
        var dirInfo = new DirectoryInfo(path);


        foreach (var subdirectory in dirInfo.GetDirectories("*.*", SearchOption.AllDirectories))
        {
            var item = new SubDirectoryItem()
            {
                Name = subdirectory.Name,
                Path = subdirectory.FullName,
                Items = GetSubItems(subdirectory.FullName, SearchOption.AllDirectories)
            };

            items.Add(item);
        }

        foreach (var file in dirInfo.GetFiles())
        {
            var item = new FileItem
            {
                Name = file.Name,
                Path = file.FullName
            };

            items.Add(item);
        }

        return items;
    }

}