|
void Main() |
|
{ |
|
// @source: http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters |
|
|
|
var path = @"C:\Temp\IOTest\Input"; |
|
var exts = new[] { "md", "txt" }; |
|
var extsAsWildcards = exts.Select(x => "*." + x).ToArray(); |
|
|
|
var getlist = getfiles(path, exts).Dump(".net 3.5 where"); |
|
var wherelist = getwhere(path, exts).Dump("where"); |
|
var splitlist = splitwhere(path, exts).Dump("split"); |
|
var globlist = globwhere(path, extsAsWildcards).Dump("glob"); |
|
|
|
wherelist.OrderBy(o => o).SequenceEqual(getlist.OrderBy(o => o)).Dump("Same Results? (.net35 vs 4)"); |
|
wherelist.OrderBy(o => o).SequenceEqual(splitlist.OrderBy(o => o)).Dump("Same Results? (where vs split)"); |
|
wherelist.OrderBy(o => o).SequenceEqual(globlist.OrderBy(o => o)).Dump("Same Results? (where vs glob)"); |
|
|
|
".net 3.5 where".Perf(n => getfiles(path, exts)); |
|
"where".Perf(n => getwhere(path, exts)); |
|
"split".Perf(n => splitwhere(path, exts)); |
|
"glob".Perf(n => globwhere(path, extsAsWildcards)); |
|
|
|
|
|
"where (tolist)".Perf(n => getwhere(path, exts).ToList()); |
|
"split (tolist)".Perf(n => splitwhere(path, exts).ToList()); |
|
"glob (tolist)".Perf(n => globwhere(path, extsAsWildcards).ToList()); |
|
|
|
} |
|
|
|
|
|
public IEnumerable<string> getfiles(string path, params string[] exts) { |
|
return |
|
Directory |
|
.GetFiles(path, "*.*") |
|
.Where(file => exts.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase))); |
|
} |
|
|
|
public IEnumerable<string> getwhere(string path, params string[] exts) { |
|
return |
|
Directory |
|
.EnumerateFiles(path, "*.*") |
|
.Where(file => exts.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase))); |
|
} |
|
|
|
public IEnumerable<string> splitwhere(string path, params string[] exts) { |
|
return |
|
exts.Select(x => "*." + x) |
|
.SelectMany(x => |
|
Directory.EnumerateFiles(path, x) |
|
); |
|
} |
|
|
|
|
|
public IEnumerable<string> globwhere(string path, params string[] globs) { |
|
return |
|
globs |
|
.SelectMany(x => |
|
Directory.EnumerateFiles(path, x) |
|
); |
|
} |
The difference between split and glob is whether you do "exts.Select(x => "*." + x)" in the test loop or outside of it. so the difference is only in testing not in real life. (unless you will do a loop with the SAME extensions in real life).