Basically this is it:
var lista = new DirectoryInfo("c:\").GetFiles("aria2c.exe", SearchOption.AllDirectories);
The secret is the second parameter of GetFiles()
that determines the recursive search with enum SearchOption
.
If you want to handle access errors on your own and avoid that the abort method can use a solution like the one below. Interestingly Marc Gravel who works at the SE has already given several answers each in a different way, I found is right for you :
public static class FileUtil {
public static IEnumerable<string> GetFiles(string root, string searchPattern) {
Stack<string> 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
}
}
}
See working on dotNetFiddle .