Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the application has access to a Directory?

Tags:

c#

.net

In my application I need to check whether or not I have permissions to write to a folder. I use the following method:

public bool IsAvailable(string path)
        {
            bool hasPermissions = false;

            if (Directory.Exists(path))
            {
                var permission = new FileIOPermission(FileIOPermissionAccess.Write, path);
                try
                {
                    permission.Demand();
                    hasPermissions = true;
                }
                catch(SecurityException e)
                {
                    hasPermissions = false;
                }
            }

            return hasPermissions;
        }

When I give it a path to a Folder that I know for certain no one has access to it (I've removed all permission for all users in the Security Tab of the Folder Properties), it doesn't throw any exception. It just continues along the try block.

Any ideas why or how to do this check better?

The AppDomain.PermissionSet Property related answers I found on other question had no succes.

Thank you in advance.

like image 606
Robert Iagar Avatar asked Mar 27 '13 09:03

Robert Iagar


People also ask

How do I check permissions for a specific directory?

To view the permissions for all files in a directory, use the ls command with the -la options. Add other options as desired; for help, see List the files in a directory in Unix. In the output example above, the first character in each line indicates whether the listed object is a file or a directory.

How do I check if a user has access to a directory in Linux?

You can run test -r /path/to/file; echo "$?" to view the return code of the test command. Use test -w to test for write permission and test -x to test for execute permission.

How do I check folder permissions in CMD?

Or to get the info of all files and folder inside that directory: PS C:\Users\Username> Dir | Get-Acl Directory: C:\Users\Username Path Owner Access ---- ----- ------ . anaconda Owner Name NT AUTHORITY\SYSTEM Allow FullControl... . android Owner Name NT AUTHORITY\SYSTEM Allow FullControl... .

How do I check permissions on a file?

Check Permissions in Command-Line with Ls Command If you prefer using the command line, you can easily find a file's permission settings with the ls command, used to list information about files/directories. You can also add the –l option to the command to see the information in the long list format.


2 Answers

I had used the following method to get it done:

public static bool HasWritePermissionOnDir(string path)
    {
        var writeAllow = false;
        var writeDeny = 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.Write & rule.FileSystemRights) != FileSystemRights.Write) continue;

            if (rule.AccessControlType == AccessControlType.Allow)
                writeAllow = true;
            else if (rule.AccessControlType == AccessControlType.Deny)
                writeDeny = true;
        }

        return writeAllow && !writeDeny;
    }

Please let me know if it helped you and if yes mark it too

like image 162
A J Avatar answered Sep 21 '22 22:09

A J


This method (ask if accessible, then do something) is prone to race conditions. Between your check and an actual access to content in that directory, the permissions may change.

Better just try to read/write something in that directory, and catch a potential exception.

So don't

if(IsAvailable(path)) {
    try {
        doSomething();
    } catch (...) {
    }
}

but rather

try {
    doSomething();
} catch (...) {
}

Grace Hopper quote:

“It’s always easier to ask forgiveness than it is to get permission.”

like image 21
Sebastian Mach Avatar answered Sep 19 '22 22:09

Sebastian Mach