14

Possible Duplicate:
.NET - Check if directory is accessible without exception handling

Im making a small file explorer in Visual Studio 2010 with NET 3.5 and C#, and I have this function to check if a directory is accessible:

RealPath=@"c:\System Volume Information";
public bool IsAccessible()
{
    //get directory info
    DirectoryInfo realpath = new DirectoryInfo(RealPath);
    try
    {
        //if GetDirectories works then is accessible
        realpath.GetDirectories();                
        return true;
    }
    catch (Exception)
    {
        //if exception is not accesible
        return false;
    }
}

But I think with big directories it could be slow trying to get all sub directories to check if directory is accesible. Im using this function to prevent errors when trying to explore protected folders or cd/dvd drives without disc ("Device Not Ready" error).

Is there a better way (faster) to check if directory is accessible by the application (preferably in NET 3.5)?

Community
  • 1
  • 1
user962284
  • 670
  • 1
  • 12
  • 29
  • 1
    Does this work for 'accessible' Directory.Exists ( Path.Combine( RealPath + "\." ) ) – kenny Jul 29 '12 at 14:10
  • You're going against the way windows explorer works. Why not show a folder that is not accessible? If the user won't see it, he won't know he has a cd drive. – Amiram Korach Jul 29 '12 at 14:12
  • 1
    http://stackoverflow.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file – Anirudha Jul 29 '12 at 14:23
  • the user doesn't know if the directory is accessible, then if the user double click the "not accessible" directory c# return an error – user962284 Jul 29 '12 at 14:24

2 Answers2

15

According to MSDN, Directory.Exists should return false if you don't have read access to the directory. However, you can use Directory.GetAccessControl for this. Example:

public static bool CanRead(string path)
{
    try
    {
        var readAllow = false;
        var readDeny = false;
        var accessControlList = Directory.GetAccessControl(path);
        if(accessControlList == null)
            return false;

        //get the access rules that pertain to a valid SID/NTAccount.
        var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
        if(accessRules ==null)
           return false;

        //we want to go over these rules to ensure a valid SID has access
        foreach (FileSystemAccessRule rule in accessRules)
        {
            if ((FileSystemRights.Read & rule.FileSystemRights) != FileSystemRights.Read) continue;

            if (rule.AccessControlType == AccessControlType.Allow)
                readAllow = true;
            else if (rule.AccessControlType == AccessControlType.Deny)
                readDeny = true;
        }

        return readAllow && !readDeny;
    }
    catch(UnauthorizedAccessException ex)
    {
        return false;
    }
}

Update

As mentioned in some comments, this may return an incorrect value in a case where a valid SID in an external DOMAIN has access. In order to check if the current user has access, you need something like:

foreach...

if (WindowsIdentity.GetCurrent().User.Value.equals(rule.IdentityReference.Value))

This will confirm if the SID of the current user matches the access rule identity reference but may throw a SecurityException as well.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • 1
    Nice. accessRules is a Collection though, and is never null, so the check is redundant. Better to check if the collection is empty. – Chris Paton Oct 20 '14 at 17:03
  • 1
    I get this error when trying to use this function. `System.UnauthorizedAccessException was unhandled HResult=-2147024891 Message=Attempted to perform an unauthorized operation.` – toddmo Oct 20 '14 at 21:30
  • This will return false if ANY user has been denied access – jbriggs Dec 01 '14 at 20:20
  • 1
    strange answer. I tried to read another user' desktop folder C:\users\another user\desktop the result was true. I tried to enter from my computer to the same folder - I could not. – Guy Cohen Apr 10 '15 at 13:26
  • I guess the first check in the `foreach` should continue if the `accessRule` at least has `FileSystemRights.ListDirectory` which is lower than `FileSystemRights.Read`. This way, an `accessRule` with `AccessControlType.Deny` on `FileSystemRights.ListDirectory` will be detected and results in `readDeny = true` – modeeb Feb 21 '18 at 06:21
  • Both `Directory.Exists` and this solution work for most cases but not all. Using `Directory.GetAccessControl` gets all access rules even if not applicable to the user running the assembly – modeeb Feb 23 '18 at 11:38
  • This doesn't work. `GetAccessControl` is throwing the exeption. – Daniel Möller Feb 12 '20 at 15:52
  • @DanielMöller The answer is a bit old but added some updates. An exception most likely means you don't have access. – Chibueze Opata Feb 12 '20 at 21:13
  • As of .NET 6 (and later), the Directory class has no longer any member called GetAccessControl(), this has been moved into the DirectoryInfo class. The MSDN documentation still states it otherwise, with a correct code sample though. – Hefaistos68 Aug 03 '23 at 12:33
0

I think you are looking for the GetAccessControl method, the System.IO.File.GetAccessControl method returns a FileSecurity object that encapsulates the access control for a file.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Rakesh
  • 273
  • 3
  • 4
  • 10