3

According to MSDN, Directory.Exists should return false if a directory is not accessible. I have a path for which Directory.Exists returns true yet Directory.GetFiles throws a System.UnauthorizedAccessException. I have also tried the CanRead function here, but this too returns true for the path.

The path is "C:\Users\{username}\AppData\Local\Microsoft\Windows\INetCache\Content.IE5" if knowing that helps.

Jonah
  • 1,495
  • 4
  • 17
  • 32
  • 1
    interesting mine is not INetCache but "temporary internet files" – BugFinder Oct 12 '17 at 14:38
  • 1
    you are misreading the documentation. `Directory.Exists` doesn't check to see if the directory is accessible, instead, it is: "true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists." – Claies Oct 12 '17 at 14:40
  • Possible duplicate of [How to handle UnauthorizedAccessException when attempting to add files from location without permissions](https://stackoverflow.com/questions/13954630/how-to-handle-unauthorizedaccessexception-when-attempting-to-add-files-from-loca) – Sinatr Oct 12 '17 at 14:41
  • @Claies "If you do not have at a minimum read-only permission to the directory, the Exists method will return false." is mentioned in the remarks section – Jonah Oct 12 '17 at 14:42
  • yes, you have `read-only` access, but that is not the permission that you need for `Directory.GetFiles`. `GetFiles` requires the `List Folder Contents` permission. – Claies Oct 12 '17 at 14:46
  • @Claies I see. Thanks. If you post an answer detailing how to check for the `List Folder Contents` permission then I'll accept it. – Jonah Oct 12 '17 at 14:48
  • you have to check the `FileSystemRights` enumeration for `ListDirectory`. see https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights(v=vs.110).aspx – Claies Oct 12 '17 at 14:51

1 Answers1

3

You don't have access to content of this folder, because first - it's actually not a folder, but a reparse point which targets another folder and second - it have quite restrictive access rights.

In your particular case this reparse point targets "C:\Users\{username}\AppData\Local\Microsoft\Windows\INetCache\IE" folder which is freely accessible.

There are several such shortcuts exists inside user folder for compatibility with legacy software. And while you cannot list content of these reparse points, you can access files and folders inside when you know the name.

And last note, you never need to check specific folder rights before access, instead you should catch UnauthorizedAccessExpception and act accordingly. You don't even need to check folder existence before access, because it can be deleted after check (not this particular folder, but in general), you simply should catch DirectoryNotFoundException.

arbiter
  • 9,447
  • 1
  • 32
  • 43