[英]Collect Directory and Sub Directories with mapping and index in a List<>
I want to collect the directory listing in a collection(a List<> perhaps) my directory structure is like : 我想将目录列表收集在一个集合中(也许是List <> ),我的目录结构是这样的:
MainFolder\ParentFolder1\SubFolder1
\SubFolder2
\SubFolder3
MainFolder\ParentFolder2\SubFolder1
\SubFolder2
\SubFolder3
I want to list all the subfolders mapped to their parent directories. 我想列出所有映射到其父目录的子文件夹。
Also, the records will have index of ParentFolder 0-n in MainFolder and index of SubFolder 0-n in each ParentFolder. 此外,记录还将在MainFolder中具有ParentFolder 0-n的 索引,在每个ParentFolder中具有SubFolder 0-n的索引。
I did tried below but not yet achieved 我在下面尝试过但尚未实现
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());
You can use this overload of the GetDirectories method to find all subdirectories recursively: 您可以使用GetDirectories方法的重载来递归查找所有子目录:
var mainDirectory = new DirectoryInfo(@"C:\temp\MainFolder");
var subDirectories = mainDirectory.GetDirectories("*", SearchOption.AllDirectories);
Then you can map them into pairs of directory/parent like this: 然后,您可以将它们映射为成对的目录/父目录,如下所示:
var mappedDirectories = subDirectories.Select(sd => new { Parent=sd.Parent, Child=sd });
If you want to exclude the first level of subdirectories ( ParentFolder1
and ParentFolder2
, in your case) you can filter them like this: 如果要排除第一级子目录(在您的情况下为ParentFolder1
和ParentFolder2
),可以按以下方式过滤它们:
var mappedDirectories = subDirectories
.Where(sd => sd.Parent.FullName != mainDirectory.FullName)
.Select(sd => new { Parent=sd.Parent, Child=sd });
EDIT after you've asked for indices: 请求索引后进行编辑:
You stated, that you'll always only have a nesting level of 2, the following piece of code will not work for deeper directory structures. 您说过,嵌套级别始终为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
} ));
This will give you a list of records, each containing 这将为您提供记录列表,每个记录包含
Try this recursive algorithm. 试试这个递归算法。
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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.