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/recover from that and continuing enumerate the rest of the files?
try
{
var files = from file in Directory.EnumerateFiles("c:",
"*.*", SearchOption.AllDirectories)
select new
{
File = file
};
Console.WriteLine(files.Count().ToString());
}
catch (UnauthorizedAccessException uEx)
{
Console.WriteLine(uEx.Message);
}
catch (PathTooLongException ptlEx)
{
Console.WriteLine(ptlEx.Message);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…