Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory is accessible in C#? [duplicate]

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)?

like image 242
user962284 Avatar asked Jul 29 '12 14:07

user962284


1 Answers

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.

like image 83
Chibueze Opata Avatar answered Oct 02 '22 03:10

Chibueze Opata