4

Hello everyone I currently got subdirectories I wanted through this call:

foreach (DirectoryInfo dir in parent)
      {
        try
        {
          subDirectories = dir.GetDirectories().Where(d => d.Exists == true).ToArray();
        }
        catch(UnauthorizedAccessException e)
        {
          Console.WriteLine(e.Message);
        }
        foreach (DirectoryInfo subdir in subDirectories)
        {
          Console.WriteLine(subdir);
          var temp = new List<DirectoryInfo>();
          temp = subdir.GetDirectories("*", SearchOption.AllDirectories).Where(d => reg.IsMatch(d.Name)).Where((d => !d.FullName.EndsWith("TESTS"))).Where(d => !(d.GetDirectories().Length == 0 && d.GetFiles().Length == 0)).Where(d => d.GetFiles().Length > 3).ToList();
          candidates.AddRange(temp);
        }
      }

      foreach(DirectoryInfo dir in candidates)
      {
        Console.WriteLine(dir);
      }

so now my issue is that my final list called candidates I get nothing because im getting an access issue due to one of the folders called lost+found in my subdirectories folder in the try block. I tried using try and catch to handle the exception so I could keep doing my checks I actually dont care about this folder and im trying to just ignore it but I'm not sure how to go about ignoring it out of my get directories search any thoughts? I already tried doing a filter with .where to ignore any folder that contained the folder name but that didnt work either it just stopped my program at the folder name.

JPhillips
  • 59
  • 1
  • 5
  • This question is already answered, see following: [link](http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access) – wake-0 May 25 '16 at 15:28
  • this is for getting files but i need directories not files – JPhillips May 25 '16 at 15:49
  • Have you tried using `Directory.EnumerateDirectories()`? It provides a bit more flexibility, and allows you to at least get the names of directories as they are found. Unfortunately, hidden system directories like `lost+found` will cause the `UnauthorizedAccessException`. Make sure you use the default `SearchOption` so the search doesn't attempt to go into any of the child directories automatically. – Berin Loritsch May 25 '16 at 16:19
  • the program fails way before that when: 'code'subDirectories = dir.GetDirectories();'code' – JPhillips May 25 '16 at 16:59

2 Answers2

2

I have the same question (ResourceContext.GetForCurrentView call exception) about this exception (UnauthorizedAccessException), and this link gives an answer to the reason why this happens:

http://www.blackwasp.co.uk/FolderRecursion.aspx

Short quote:

... Key amongst these is that some of the folders that you attempt to read could be configured so that the current user may not access them. Rather than ignoring folders to which you have restricted access, the method throws an UnauthorizedAccessException. However, we can circumvent this problem by creating our own recursive folder search code. ...

solution:

private static void ShowAllFoldersUnder(string path, int indent)
{
    try
    {
        foreach (string folder in Directory.GetDirectories(path))
        {
            Console.WriteLine("{0}{1}", new string(' ', indent), Path.GetFileName(folder));
            ShowAllFoldersUnder(folder, indent + 2);
        }
    }
    catch (UnauthorizedAccessException) { }
}
1

You can use recursion like Microsoft explains: link.

wake-0
  • 3,918
  • 5
  • 28
  • 45
  • how is this different than I do it, I'm simply using a try and catch block at the area where my prog is failing and I've tried multiple ways to handle the catch part but it still fails and does not ignore the one fail. – JPhillips May 25 '16 at 17:01
  • If you get the directories before you iterate through them, while trying the getDirectories call, the catch won't affect the foreach and break out of it – Ben Philipp Jan 25 '18 at 20:31