【问题标题】:c# Directories Files, process each top folder individually with its respective sub folders and filesc# Directories Files,分别处理每个顶级文件夹及其各自的子文件夹和文件
【发布时间】:2018-10-31 00:42:28
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

  • 你能提供更多代码来说明这个问题吗?我真的不明白你在做什么或你在哪里遇到问题。此外,您可能需要选择更具描述性的问题标题。
  • LocalFile 到底是什么?
  • 本地文件只是一个对象,包含两个属性,文件的相对路径和文件的流。
  • 问题是,我需要单独处理每个顶级文件夹。函数 GetLocal() 抛出一个访问被拒绝的异常,这是因为文件流仍然是打开的,如果我将 file.openread() 包装在一个使用它抛出另一个异常不能访问一个关闭的流,如上所述。

标签: c# linq file directory


【解决方案1】:

终于明白了……

新建了一个类

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

我希望这对遇到类似情况的人有所帮助。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多