1

How to bypass/ignore "Access to the path is denied"/UnauthorizedAccess exception

and continue to collecting filenames in this method;

public static string[] GetFilesAndFoldersCMethod(string path)
{
   string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray();
   return filenames;
}

//Calling... ...

foreach (var s in GetFilesAndFoldersCMethod(@"C:/"))
{
    Console.WriteLine(s);
}

My application stops on the firstline of GetFilesAndFoldersCMethod and an exception says; "Access to the path 'C:\@Logs\' is denied.". Please help me...

Thanks,

Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
  • Doesn't look to me like you can request that this method continue enumerating when it encounters an error. I think you'll have to roll your own enumerator or find another class that is more flexible. – David Heffernan Jan 16 '12 at 09:21

1 Answers1

8

Best way to do this is to add a Try/Catch block to handle the exception...

try
{
   string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray();
   return filenames;
}
catch (Exception ex)
{
   //Do something when you dont have access
   return null;//if you return null remember to handle it in calling code
}

you can also specifically handle the UnauthorizedAccessException if you are doing other code in this function and you want to make sure it is an access exception that causes it to fail (this exception is thrown by the Directory.GetFiles function)...

try
{
   //...
}
catch(UnauthorizedAccessException ex)
{
    //User cannot access directory
}
catch(Exception ex)
{
    //a different exception
}

EDIT: As pointed out in the comments below it appears you are doing a recursive search with the GetFiles function call. If you want this to bypass any errors and carry on then you will need to write your own recursive function. There is a great example here that will do what you need. Here is a modification which should be exactly what you need...

List<string> DirSearch(string sDir) 
{
   List<string> files = new List<string>();

   try  
   {
      foreach (string f in Directory.GetFiles(sDir)) 
      {
         files.Add(f);
      }

      foreach (string d in Directory.GetDirectories(sDir)) 
      {
         files.AddRange(DirSearch(d));
      }
   }
   catch (System.Exception excpt) 
   {
      Console.WriteLine(excpt.Message);
   }

   return files;
}
musefan
  • 47,875
  • 21
  • 135
  • 185
  • Surely the enumeration will stop there too – David Heffernan Jan 16 '12 at 09:18
  • @DavidHeffernan: Yes, but the enumeration is on a string array - which is all files in the given directory, and as the access exception is thrown for the directory then all files will be inaccessible anyway. My assumption is that if the user is searching multiple directories then they have a loop/recursive function around the example for loop. Of course, I could be assuming wrong, will have to wait for an OP response to be sure – musefan Jan 16 '12 at 09:23
  • @DavidHeffernan: My mistake, I now see that the GetFiles call is doing the recursive work – musefan Jan 16 '12 at 09:24
  • 1
    Thank you very much for the answer. Please look at the first foreach; foreach (string f in Directory.GetFiles(d)) -> value d must be "sDir". – Lost_In_Library Jan 16 '12 at 11:31
  • Just a note, although the above example should work fine, if this code were to be implemented you should consider catching only UnauthorizedAccessException. As the code is now, it may give results that is hard to debug if some unexpected error should occur. (Only catch exceptions you know you can recover from!) – Niclas Jan 17 '12 at 08:52
  • @Niclas: I did already mention this is my post. The final piece of code was just a modification of the code found at the link I suggested – musefan Jan 17 '12 at 09:05
  • @musefan: Yes, i saw that, but you didn't really stress it and while I'm sure you are aware of the problems that could arise from catching to generic exceptions not everyone are and I thought it was worth pointing out since it can be hard to debug... – Niclas Jan 17 '12 at 09:35
  • The answer [here](https://stackoverflow.com/questions/4986293/access-to-the-path-is-denied-when-using-directory-getfiles) has a much more complete solution by implementing a custom iterator and doesn't use recursion, better memory footprint, also the try/catch just doesn't work in some cases. – sean_m Nov 25 '15 at 21:59