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

时间:2018-10-31 00:42:28

标签: c# linq file directory

我正在尝试将每个文件夹,子文件夹和文件放入路径之外的对象中。

此路径将包含许多顶级文件夹以及许多子文件夹和文件。

我需要分别处理每个顶层文件夹及其子文件夹和文件。

example 

C:\ temp \ Folder1 \ SubFolder \ SubFiles
C:\ temp \ Folder2 \ SubFiles \ SubFolders \ SubFiles

需要处理Folder1及其子目录,并将其包含在一个对象中。与文件夹2相同。

这是一些代码,由于该流保持打开状态,因此访问被拒绝,如果我将其包装在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();
}

谢谢

1 个答案:

答案 0 :(得分:0)

最后知道了...

开设了新班级

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

这是代码

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

我希望这可以帮助遇到类似情况的人。

谢谢