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

2

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

Idiocy answered 29/7, 2012 at 14:7 Comment(4)
Does this work for 'accessible' Directory.Exists ( Path.Combine( RealPath + "\." ) )Kitty
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.Granada
#131117Postrider
the user doesn't know if the directory is accessible, then if the user double click the "not accessible" directory c# return an errorIdiocy
A
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.

Aldas answered 29/7, 2012 at 14:52 Comment(9)
Nice. accessRules is a Collection though, and is never null, so the check is redundant. Better to check if the collection is empty.Intercellular
I get this error when trying to use this function. System.UnauthorizedAccessException was unhandled HResult=-2147024891 Message=Attempted to perform an unauthorized operation.Verisimilar
This will return false if ANY user has been denied accessVersion
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.Degradation
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 = trueInscribe
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 assemblyInscribe
This doesn't work. GetAccessControl is throwing the exeption.Local
@DanielMöller The answer is a bit old but added some updates. An exception most likely means you don't have access.Aldas
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.Brucine
L
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.

Lynnett answered 29/7, 2012 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.