简体   繁体   English

c#目录文件,使用其各自的子文件夹和文件分别处理每个顶层文件夹

[英]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.

相关问题 Rackspace CloudFiles C#API:如何在根“文件夹”中列出文件,以及如何在“文件夹”中列出(子)文件夹? - Rackspace CloudFiles C# API: How to list files on root 'folder', and how to list (sub)folders within a 'folder'? 从文件夹,其子文件夹及其中的所有文件中删除只读 - removing readonly from folder, its sub folders and all the files in it C#删除一个文件夹以及该文件夹内的所有文件和文件夹 - C# delete a folder and all files and folders within that folder 将三个子目录中的文件添加到C#中自己的列表中 - Add files from three sub directories into their own Lists in C# 使用c#和线程删除大量文件和子文件夹 - Delete a lot of files and sub folders with c# and threads C#将目录中每个文件的MD5以及所有嵌套/子/子目录中的文件保存到文本文件中 - C# save MD5 of each file in a directory, as well as files in all nested/child/sub directories, to a text file 如何按部分名称c#查找文件夹和文件 - How to find folders and files by its partial name c# 在C#中有条件地将文件目录和子目录移动到另一个目录(仅移动最近5天创建的文件和目录) - Move files directories and sub directories to another directory Conditionally (Move only Last 5 days created files and directories) in C# 如何删除所有文件/文件夹但将根文件夹保留在 C# 中 - How to delete all files/folders but keep the root folder in C# 如何将文件从文件夹和子文件夹传输到 C# 中由文件名模式创建的特定文件夹(File2specific Folders) - How to transfer files form folders and sub-folders to specific folders created by file name pattern in C# (File2specific Folders)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM