c# - 最快/最安全的文件查找/解析?

标签 c# .net file parsing search

c: 上,我有数以万计的 *.foobar 文件。它们在各种各样的地方(即子目录)。这些文件的大小大约为 1 - 64 kb,并且是纯文本。

我有一个 class Foobar(string fileContents) 可以强类型化这些 .foobar 文件。

我的挑战是获取 c: 上所有 *.foobar 文件的列表,表示为 Foobar 对象数组。最快的方法是什么?

我很想知道是否有比我的第一种方法更好的方法(毫无疑问),以及我的这种方法是否有任何潜在问题(例如 I/O 并发问题引发异常?):

var files = Directory.EnumerateFiles
                (rootPath, "*.foobar", SearchOption.AllDirectories);

Foobar[] foobars = 
(
    from filePath in files.AsParallel()
    let contents = File.ReadAllText(filePath)
    select new Foobar(contents)
)
.ToArray();

最佳答案

因为权限错误(或其他错误)显然会使枚举停止在其轨道上,您可能想要实现自己的枚举器,如下所示:

class SafeFileEnumerator : IEnumerable<string>
{
  private string root;
  private string pattern;
  private IList<Exception> errors;
  public SafeFileEnumerator(string root, string pattern)
  {
     this.root = root;
     this.pattern = pattern;
     this.errors = new List<Exception>();
  }

  public SafeFileEnumerator(string root, string pattern, IList<Exception> errors)
  {
     this.root = root;
     this.pattern = pattern;
     this.errors = errors;
  }

  public Exception[] Errors()
  {
     return errors.ToArray();
  }
  class Enumerator : IEnumerator<string>
  {
     IEnumerator<string> fileEnumerator;
     IEnumerator<string> directoryEnumerator;
     string root;
     string pattern;
     IList<Exception> errors;

     public Enumerator(string root, string pattern, IList<Exception> errors)
     {
        this.root = root;
        this.pattern = pattern;
        this.errors = errors;
        fileEnumerator = System.IO.Directory.EnumerateFiles(root, pattern).GetEnumerator();
        directoryEnumerator = System.IO.Directory.EnumerateDirectories(root).GetEnumerator();
     }
     public string Current
     {
        get
        {
           if (fileEnumerator == null) throw new ObjectDisposedException("FileEnumerator");
           return fileEnumerator.Current;
        }
     }

     public void Dispose()
     {
        if (fileEnumerator != null)
           fileEnumerator.Dispose();
        fileEnumerator = null;
        if (directoryEnumerator != null)
           directoryEnumerator.Dispose();
        directoryEnumerator = null;
     }

     object System.Collections.IEnumerator.Current
     {
        get { return Current; }
     }

     public bool MoveNext()
     {
        if ((fileEnumerator != null) && (fileEnumerator.MoveNext()))
           return true;
        while ((directoryEnumerator != null) && (directoryEnumerator.MoveNext()))
        {
           if (fileEnumerator != null)
              fileEnumerator.Dispose();
           try
           {
              fileEnumerator = new SafeFileEnumerator(directoryEnumerator.Current, pattern, errors).GetEnumerator();
           }
           catch (Exception ex)
           {
              errors.Add(ex);
              continue;
           }
           if (fileEnumerator.MoveNext())
              return true;
        }
        if (fileEnumerator != null)
           fileEnumerator.Dispose();
        fileEnumerator = null;
        if (directoryEnumerator != null)
           directoryEnumerator.Dispose();
        directoryEnumerator = null;
        return false;
     }

     public void Reset()
     {
        Dispose();
        fileEnumerator = System.IO.Directory.EnumerateFiles(root, pattern).GetEnumerator();
        directoryEnumerator = System.IO.Directory.EnumerateDirectories(root).GetEnumerator();
     }
  }
  public IEnumerator<string> GetEnumerator()
  {
     return new Enumerator(root, pattern, errors);
  }

  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  {
     return GetEnumerator();
  }
}

关于c# - 最快/最安全的文件查找/解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9746538/

相关文章:

c# - Xamarin PCL Refit 3.0.1 , 看起来不像 Refit 界面

c# - 如何创建接收 XML 的 Web 服务?

Python 文件,获取文件并查找平均值

file - 将文本文件读入 Applescript 中的列表

c# - 您如何将 Java 解决方案组织到多个项目中,就像在 Visual Studio 中一样?

c# - 有什么简单的方法可以将 .xls 文件转换为 .csv 文件吗? (Excel)

c# - System.Reflection.BindingFlags.Instance 与 C# 访问修饰符的对应关系

.net - .Net 中的 MCRYPT

exception - 派生自异常类警告 : CA2237: Mark ISerializable types with SerializableAttribute

c - 在c中打开/读取文本文件