use*_*672 5 .net c# search parsing file
在c:
,我有成千上万的*.foobar
文件.他们在各种各样的地方(即子市场).这些文件大小约为1 64 kb,并且是纯文本.
我有一个class Foobar(string fileContents)
强烈键入这些.foobar
文件.
我的挑战是获取所有*.foobar
文件的列表c:
,表示为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();
Run Code Online (Sandbox Code Playgroud)
因为权限错误(或其他错误)显然可以阻止枚举在其轨道中死亡,您可能希望实现自己的枚举器,如下所示:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1720 次 |
最近记录: |