使用List<>中的映射和索引收集目录和子目录。

时间:2015-07-03 10:16:50

标签: c# linq list collections directory-structure

我想收集集合中的目录列表(可能是 List<> ) 我的目录结构如下:

MainFolder\ParentFolder1\SubFolder1    
                        \SubFolder2    
                        \SubFolder3

MainFolder\ParentFolder2\SubFolder1
                        \SubFolder2
                        \SubFolder3

我想列出映射到其父目录的所有子文件夹。

此外,记录将在MainFolder 中包含的ParentFolder 0-n索引,并在每个ParentFolder中包含索引的SubFolder 0-n。

我在下面尝试但尚未实现

lstParents = (from f in Directory.GetDirectories(MainFolder)
             select  Data
         {
        parent =f
         }).ToList();

var lstSubDir = (from f in lstParents.Select(m => Directory.GetDirectories(m.parent).ToList());

2 个答案:

答案 0 :(得分:2)

您可以使用GetDirectories方法的this overload递归查找所有子目录:

var mainDirectory = new DirectoryInfo(@"C:\temp\MainFolder");
var subDirectories = mainDirectory.GetDirectories("*", SearchOption.AllDirectories);

然后你可以将它们映射成目录/父对,如下所示:

var mappedDirectories = subDirectories.Select(sd => new { Parent=sd.Parent, Child=sd });

如果要排除第一级子目录(ParentFolder1ParentFolder2,在您的情况下),您可以像这样过滤它们:

var mappedDirectories = subDirectories
    .Where(sd => sd.Parent.FullName != mainDirectory.FullName)
    .Select(sd => new { Parent=sd.Parent, Child=sd });

在你要求索引之后编辑:

你说,你总是只有2的嵌套级别,下面的代码段不适用于更深层的目录结构。

var mainDirectory = new DirectoryInfo(@"C:\temp\MainFolder");
var firstLevelDirectories = mainDirectory.GetDirectories().Select((f1,i) => new { 
  Parent = f1, 
  ParentIndex = i 
});

var secondLevelDirectories = firstLevelDirectories
  .SelectMany(f1 => f1.Parent.GetDirectories().Select((f2,i) => new {
    f1.Parent, 
    f1.ParentIndex, 
    Child = f2, 
    ChildIndex = i
} ));

这将为您提供一个记录列表,每个记录包含

  • 父目录
  • 父目录索引
  • 子目录和
  • 其父目录中的子目录索引。

答案 1 :(得分:0)

尝试这种递归算法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Folders folders = new Folders(@"c:\temp", null);
            Console.ReadLine();
        }
    }
    public class Folders
    {
        public string path { get; set; }
        List<string> files = new List<string>();
        List<Folders> folders = new List<Folders>();
        Folders parent = null;

        public Folders(string path, Folders parent)
        {
            this.parent = parent;
            this.path = path;
            foreach (string folderPath in Directory.GetDirectories(path))
            {
                Folders newFolder = new Folders(folderPath, this);
                folders.Add(newFolder);
            }
            files = Directory.GetFiles(path).ToList();
            int pathlength = path.Length;
            Boolean first = true;
            Console.Write(path);
            if (files.Count == 0) Console.WriteLine();
            foreach (string file in files)
            {
                string shortname = file.Substring(file.LastIndexOf("\\") + 1);
                if (first)
                {
                    Console.WriteLine("\\" + shortname);
                    first = false;
                }
                else
                {

                    Console.WriteLine(new string(' ', pathlength + 1) + shortname);
                }
            }

        }
    }
}
​