在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)