I am iterating an IEnumerable
from EnumerateFileSystemEntries
where the act of getting the next element could throw an access denied exception. So I wrap it in a try but I don't want to stop iterating just because one element in the middle is bad. There are more elements that are good after the bad one. So I put a continue
in my catch but that's not valid. Is there a way to do what I am failing to do here?
paths = Directory.EnumerateFileSystemEntries(RootDirectory, SearchPattern, SearchOption);
try
{
foreach (var path in paths)
{
// Do something with the paths
}
} catch (Exception ex)
{
Debug.Print(ex.Message);
continue; // ERROR: "No enclosing loop out of which to break or continue"
}
I have gone through every answer in 3 duplicate posts. They all suffer from the same problem. The only solution I have seen is to port my application to .NET core which is a long process: https://learn.microsoft.com/en-us/dotnet/core/porting/
If you came here looking for a FrameWork based solution, abandon all hope. Nobody here will help you. If you do come up with a good answer you can't even add it because all of the other posts have flawed solutions accepted.