0

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.

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • 3
    put try catch INSIDE foreach – Adassko Jul 30 '20 at 18:31
  • 2
    Move the `try..catch` block inside the loop. – 41686d6564 stands w. Palestine Jul 30 '20 at 18:31
  • Does this answer your question? [Why should I put try/catch block out of loop?](https://stackoverflow.com/questions/7928050/why-should-i-put-try-catch-block-out-of-loop) – 41686d6564 stands w. Palestine Jul 30 '20 at 18:33
  • 1
    No. The exception occurs on the `foreach` line. The access denied happens when it attempts to get the next `path` from the enumerator. – HackSlash Jul 30 '20 at 18:36
  • @HackSlash Oh, my bad. I didn't notice that you were enumerating files. Well, this is a known issue when trying to enumerate files/directories with the `AllDirectories` option. The duplicate post would be [this one](https://stackoverflow.com/q/172544/8967612) in that case. – 41686d6564 stands w. Palestine Jul 30 '20 at 18:38
  • What's the *actual* error? – Panagiotis Kanavos Jul 30 '20 at 18:39
  • @PanagiotisKanavos The actual error is `System.UnauthorizedAccessException` – HackSlash Jul 30 '20 at 18:39
  • I quite like this answer: https://stackoverflow.com/a/10321814/336378 it basically wrapps the yield call of `EnumerateFileSystemEntries`. There is also an option about skipping these files altogether. – Alex AIT Jul 30 '20 at 18:40
  • @AhmedAbdelhameed, I have looked at the duplicate post you cite. The accepted solution fails without returning the paths after the error. Same problem. I'm not sure how it was accepted when the results are unacceptable. – HackSlash Jul 30 '20 at 18:40
  • 1
    @HackSlash The answer clearly states "Untested, but something like below", so, it's meant to give you an idea on how to go about the problem, not a copy-paste solution – Camilo Terevinto Jul 30 '20 at 18:41
  • @HackSlash in that case the solution is to use `EnumerationOptions` and tell the method to ignore inaccessible files – Panagiotis Kanavos Jul 30 '20 at 18:42
  • @PanagiotisKanavos `EnumerationOptions` are new in .NET 5 and I'm not using unstable framework versions. – HackSlash Jul 30 '20 at 18:43
  • It's like none of you have actually done this yourselves. – HackSlash Jul 30 '20 at 18:45
  • @HackSlash no, they're available since .NET Core 2.1 - the previous Long-Term-Support version. The current LTS version is .NET Core 3.1. And yes, I've done this already to scan disk drives and analyse files – Panagiotis Kanavos Jul 30 '20 at 18:45
  • @HackSlash What Camilo said + there are other answers in the linked post with working examples. – 41686d6564 stands w. Palestine Jul 30 '20 at 18:46
  • The problem with this question is that it *didn't* ask about the real problem but the attempted solution. That's called [the XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and leads to delays and confusion. When the *real* problem was discovered it was easy to find a duplicate question - even though the best answer is the very last one – Panagiotis Kanavos Jul 30 '20 at 18:47
  • @PanagiotisKanavos so your answer is that I need to stop using FrameWork? – HackSlash Jul 30 '20 at 18:48
  • @HackSlash you didn't specify you want a solution for .NET Framework. In any case, you'll have to switch eventually to .NET Core - .NET 5 is essentially .NET Core 5. .NET Framework is no longer being developed – Panagiotis Kanavos Jul 30 '20 at 18:50
  • @HackSlash the duplicate shows how to handle this in .NET Framework too - you can't simply use a `foreach` loop. – Panagiotis Kanavos Jul 30 '20 at 18:50
  • @PanagiotisKanavos those solutions all skip everything after the first exception is raised in a given folder, files or folders. They all boil down to the exact same problem. You can continue to the next folder but you miss everything in the folder where an exception is raised. – HackSlash Jul 30 '20 at 18:53
  • @HackSlash .NET Framework .NET 4.0 is no longer supported and quite likely, not the runtime you're using at all. The earliest supported version is .NET 4.5.2. .NET 4.x versions are binary replacements which means a Windows Update may have already replaced the .NET 4.0 runtime with a newer one. No supported Windows version requires .NET 4.0 either. The only one that did was Windows XP, which is no longer supported – Panagiotis Kanavos Jul 30 '20 at 18:53
  • @HackSlash `those solutions all skip everything after the first exception` you didn't ask for something else. What your question asks is how to skip errors when enumerating files. The common reason for this to happen during enumeration is trying to access a system folder. If you don't have permission to read a folder or file, you won't even see it in the enumeration – Panagiotis Kanavos Jul 30 '20 at 18:55
  • @HackSlash I strongly suggest posting a different question with the *actual* problem - do you get this problem with files or folders? Which files, which folders? Are those system folders? Or specific files inside a folder that should be accessible? – Panagiotis Kanavos Jul 30 '20 at 18:56
  • Starting with the ` C:\ ` drive, for example. When it gets to any system directory the iteration stops and you can't retrieve the rest of the folders that you do have permission to. – HackSlash Jul 30 '20 at 18:58
  • 1
    You might be interested in the AlphaFs nuget package which has wrappers around all the built in IO types. For directory/file enumeration it has options to skip invalid/inaccessible entries that it gets around using native interop (hidden to the user). No affiliation, just a happy customer – pinkfloydx33 Jul 31 '20 at 00:01

0 Answers0