UnauthorizedAccessException with getDirectories

Hi everyone, I currently have subdirectories that I wanted to get 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 problem is that my final list, called candidates, I get nothing, because I get an access problem due to one of the folders named lost + found in my subdirectory folder in the try block. I tried using try and catch to handle the exception, so I could continue to do my checks. Actually, I don't care about this folder, and I'm trying to just ignore it, but I'm not sure how to get around this without my search. thoughts? I already tried to do a filter with. Where to ignore any folder containing the folder name, but this does not work, or just stopped my program in the folder name.

+4
2

, Microsoft: .

0

( ResourceContext.GetForCurrentView) (UnauthorizedAccessException), ,

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

:

... , , , . , , , UnauthorizedAccessException. , ....

:

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) { }
}
0

All Articles