In .NET 4, there\'s this Directory.EnumerateFiles() method with recursion that seems handy.
However, if an Exception occurs within a recursion, how can I continue/recove
I did found a solution to this. By using a stack to push the enumeration results, one can indeed handle the exceptions. Here's a code snippet: (inspired by this article)
List results = new List();
string start = "c:\\";
results.Add(start);
Stack stack = new Stack();
do
{
try
{
var dirs = from dir in Directory.EnumerateDirectories(
start, "*.*", SearchOption.TopDirectoryOnly)
select dir;
Array.ForEach(dirs.ToArray(), stack.Push);
start = stack.Pop();
results.Add(start);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine(ex.Message);
start = stack.Pop();
results.Add(start);
}
} while (stack.Count != 0);
foreach (string file in results)
{
Console.WriteLine(file);
}