Is there a way of recover from an Exception in Directory.EnumerateFiles ?
Posted
by Magnus Johansson
on Stack Overflow
See other posts from Stack Overflow
or by Magnus Johansson
Published on 2010-04-01T08:29:37Z
Indexed on
2010/04/01
8:33 UTC
Read the original article
Hit count: 892
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);
}
© Stack Overflow or respective owner