Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I noticed that when I call 'Directory.Exists()' on a folder that is beneath another folder whose access is denied to the user, it returns False even though the directory technically exists.

For example, assume I have two folders:

C:\FirstFolder\
C:\FirstFolder\SecondFolder\

If I place a security entry on 'C:\FirstFolder\' so that the user is explicitly denied 'read' permissions on that folder, I cannot browse beneath that folder in Windows Explorer (which is the understood behavior).

If I write some code that calls 'Directory.Exists("C:\FirstFolder\SecondFolder\")', then it returns False. This result is misleading because the directory does actually exist. With most other IO operations (such as 'Directory.Delete()'), I would get an 'UnauthorizedAccessException' or an 'IOException' or something like that. Why not from the 'Directory.Exists()' method?

Does anyone know of an easy way around this?
Posted

Here is my simple solution for checking listable but unreadable network folders:

C#
public static bool DirectoryVisible(string path)
{
    try
    {
        Directory.GetAccessControl(path);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return true;
    }
    catch
    {
        return false;
    }
}
 
Share this answer
 
I don't think there is an easy way around this. From a security point of view it makes sense. If a user is not allowed read access to a directory, then they shouldn't be able to get any information about the directory at all including subdirectory structure. Also it's philosophically correct, because to the user the directory doesn't exist.
 
Share this answer
 
The DirectoryInfo.Attributes property is incorrectly documented and does not raise the FileNotFound or DirectoryNotFound exceptions but instead returns the error value from the underlying win api function, which is 0xFFFFFFFF or -1.

If a path exists but access is disallowed then an attempt to retrieve the attributes will throw an exception.

If the path does not exist then the attributes will be -1.

Examples
C:\Documents and Settings\Administrator\Desktop
  This is not accessible to a standard user account
  DirInfo.Exists = false
  DirInfo.Attributes throws Access Denied

C:\Documents and Settings\Administrator\Desktop\NoDir
  This does not exist and would not be accessible
  DirInfo.Exists = false
  DirInfo.Attributes  =  -1

Test Code (run on XP Pro SP 3)
C#
    enum ExistState { exist, notExist, inaccessible };
    
    void Check(string name) {
      DirectoryInfo di = new DirectoryInfo(name);
      ExistState state = ExistState.exist;
      if (!di.Exists) {
        try {
          if ((int)di.Attributes == -1) {
            state = ExistState.notExist;
          }
        } catch (UnauthorizedAccessException) {
          state = ExistState.inaccessible;
        }
      }
      Console.WriteLine("{0} {1}", name, state);
    }

Output:
  C:\Documents and Settings\Administrator\Desktop inaccessible
  C:\Documents and Settings\Administrator\Desktop\NoDir notExist


I doubt that MS will fix this failing of the Attributes property to behave as documented any time soon but having said that I'm using .NET Framework 2 and am assuming that nothing has changed in the more recent versions.

One problem that I can see in my code is that it is impossible to know whether name refers to a file or a directory if the state value is 'inaccessible'.

Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900