Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
We have a scenario in our client project where the client application saves and tries to fetch files from a shared folder location , which is a virtual shared folder. But we have been facing issues with a specific application user id losing access to that shared folder. So we need help in pre advance checking if the access is present for that user id on that shared folder location . So thinking of an application to be developed in C# or vb.net to check for that user access.
Posted
Comments
Andy Lanng 31-Jul-15 10:19am    
You can have an app running as that user that tried to get a file list from the directory. Catch the permission exception to report that the user has no access.
infy504 31-Jul-15 10:27am    
Thanks Andy for your input. As you said we can try that way too. If you have any such .net code can you share .
Andy Lanng 31-Jul-15 10:28am    
I have not

1 solution

Use below function

C#
public static bool CanRead(string path)
{
    var readAllow = false;
    var readDeny = false;
    var accessControlList = Directory.GetAccessControl(path);
    if(accessControlList == null)
        return false;
    var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
    if(accessRules ==null)
       return false;

    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;
}
 
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