0

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

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

我一直在尝试组合两种不同的递归方法,以便将子文件夹检索为“SubDirectoryItems”而不是“DirectoryItem”由于缺乏澄清,我将放弃该代码尝试,但我想知道是否有更好的方法?

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

}

}

4

1 回答 1

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

}
于 2016-09-16T02:06:24.443 回答