A call to Directory.EnumerateFiles(..) will only result in a counter setting due to lazy evaluation. This is when you execute it using foreach , which you can throw an exception.
So, you need to make sure that the exception is handled in the right place so that the enumeration continues.
var files = from file in Directory.EnumerateFiles("c:\\", "*.*", SearchOption.AllDirectories) select new { File = file }; foreach (var file in files) { try { Console.Writeline(file); } catch (UnauthorizedAccessException uEx) { Console.WriteLine(uEx.Message); } catch (PathTooLongException ptlEx) { Console.WriteLine(ptlEx.Message); } }
Update : there is additional information in this matter
Matt Warren Apr 01 '10 at 10:06
source share