[英]c# Directories Files, process each top folder individually with its respective sub folders and files
I am trying to get each folder , sub folders and files into an object out of a path. 我正在尝试将每个文件夹,子文件夹和文件放入路径之外的对象中。
This path will have many top level folder with many sub folders and files. 此路径将包含许多顶级文件夹以及许多子文件夹和文件。
I need to process each top folder and its sub folders and files separately. 我需要分别处理每个顶层文件夹及其子文件夹和文件。
example
C:\\temp\\Folder1\\SubFolder\\SubFiles C:\\ TEMP \\ Folder1中\\的子文件夹\\子文件
C:\\temp\\Folder2\\SubFiles\\SubFolders\\SubFiles C:\\ TEMP \\文件夹2 \\子文件\\子文件夹\\子文件
Need to process Folder1 with its sub directories and enclose it into an object. 需要处理Folder1及其子目录,并将其包含在一个对象中。 and same for folder 2.
与文件夹2相同。
here is some code , getting access denied because the stream remains open and if I wrap the stream in a using , its closed before I can process the information. 这是一些代码,由于流保持打开状态,因此拒绝访问,因为如果我将流包装在using中,则在处理信息之前将其关闭。
private static IEnumerable<LocalFile> GetLocalFile(string source)
{
return Directory.GetDirectories(source)
.SelectMany(m =>
{
return Directory.EnumerateFileSystemEntries(m, "*", SearchOption.AllDirectories)
.Select(x =>
{
var relative = x.Substring(source.Length + 1);
var localFile = new LocalFile(relative,
() =>
{
return File.OpenRead(x);
});
return localFile;
});
});
}
public sealed class LocalFile
{
private readonly Func<Task<Stream>> openLocal;
public LocalFile(string relativePath, Func<Stream> openLocal)
{
if (openLocal == null)
{
throw new ArgumentNullException(nameof(openLocal));
}
this.RelativePath = relativePath ?? throw new ArgumentNullException(nameof(relativePath));
this.openLocal = () => Task.FromResult(openLocal());
}
public LocalFile(string relativePath, Func<Task<Stream>> openLocal)
{
this.RelativePath = relativePath ?? throw new ArgumentNullException(nameof(relativePath));
this.openLocal = openLocal ?? throw new ArgumentNullException(nameof(openLocal));
}
public string RelativePath { get; }
public Task<Stream> OpenLocal() => this.openLocal();
}
Thank you 谢谢
Finally got it... 终于明白了...
Made a new class 上了新课
public class LocalFolder
{
public string FolderPath { get; }
public IEnumerable<LocalFile> FolderFiles { get; }
public LocalFolder(string folderPath, IEnumerable<LocalFile> files)
{
this.FolderPath = folderPath ?? throw new ArgumentNullException(nameof(folderPath));
this.FolderFiles = files ?? throw new ArgumentNullException(nameof(files));
}
}
here is the code 这是代码
private static IEnumerable<LocalFolder> GetFolderFiles(string source)
{
var directories = Directory.EnumerateFileSystemEntries(source);
var folderFiles = directories
.Where(d => !d.Contains("."))
.Select(m =>
{
var files = Directory.EnumerateFiles(m, "*", SearchOption.AllDirectories)
.Select(f =>
{
var relative = f.Substring(source.Length + 1);
return new LocalFile(relative, () => File.OpenRead(f));
});
return new LocalFolder(m,
files);
});
var filesinDir = directories
.Where(d => d.Contains("."))
.Select(m =>
{
var files = Directory.EnumerateFiles(source, "*", SearchOption.TopDirectoryOnly)
.Select(f =>
{
var relative = f.Substring(source.Length + 1);
return new LocalFile(relative, () => File.OpenRead(f));
});
return new LocalFolder(m,
files);
});
return folderFiles.Concat(filesinDir);
}
I hope this helps someone whom encounters a similar situation. 我希望这对遇到类似情况的人有所帮助。
Thank you 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.