Basically this is it:
var lista = new DirectoryInfo("c:\\").GetFiles("aria2c.exe", SearchOption.AllDirectories);
The secret is the second parameter of GetFiles()
which determines the recursive search with the enumeration SearchOption
.
If you want to handle access errors on your own and prevent the abort method can use a solution like the one below. Interestingly Marc Gravel who works at SE has already given several answers each in a different way, I found is the most suitable for you:
using static System.Console;
using System.IO;
using System.Collections.Generic;
public class Program {
public static void Main() {
foreach (var file in FileUtil.GetFiles("c:\\", "aria2c.exe")) WriteLine(file);
}
}
public static class FileUtil {
public static IEnumerable<string> GetFiles(string root, string searchPattern) {
var pending = new Stack<string>();
pending.Push(root);
while (pending.Count != 0) {
var path = pending.Pop();
string[] next = null;
try {
next = Directory.GetFiles(path, searchPattern);
}
catch { } //aqui você pode colocar log, aviso ou fazer algo útil se tiver problemas
if (next != null && next.Length != 0) foreach (var file in next) yield return file;
try {
next = Directory.GetDirectories(path);
foreach (var subdir in next) pending.Push(subdir);
}
catch { } //aqui você pode colocar log, aviso ou fazer algo útil se tiver problemas
}
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.