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

Collect Directory and Sub Directories with mapping and index in a List<>

我想将目录列表收集到一个集合中(也许 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());

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

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

然后你可以像这样将它们映射成 directory/parent 对:

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

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

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

试试这个递归算法。

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

        }
    }
}
​