An interesting thing for me was to indicate what is the fastest way, and so I tried 3 methods:
Overview
* ~ 200 Ticks
Conclusion
Parallel approximation works best in this case, Directory.EnumerateFilesmuch faster than Directory.GetFiles(search using the * .txt template and subdirectories)
the code
A - GetFiles and ReadAllText
foreach (var file in Directory.GetFiles("C:\\Program Files (x86)", "*.txt", SearchOption.AllDirectories))
{
var a = File.ReadAllText(file);
}
B - EnumerateFiles and ReadAllText
foreach (var file in Directory.EnumerateFiles("C:\\Program Files (x86)", "*.txt", SearchOption.AllDirectories))
{
var a = File.ReadAllText(file);
}
C - Parallel Approach
var files = Directory.EnumerateFiles("C:\\Program Files (x86)", "*.txt", SearchOption.AllDirectories);
Parallel.ForEach(files,(current) =>
{
var a = File.ReadAllText(current);
});
FEEL FREE to add ideas, thoughts, ....
source
share