Checking file/folder access permission

40,160

Solution 1

First of all, I would manually check the permissions and see what blocks you and what doesn't. I am using something like this to check for permissions (for copy file):

AuthorizationRuleCollection acl = fileSecurity.GetAccessRules(true, true,typeof(System.Security.Principal.SecurityIdentifier));
bool denyEdit = false;
for (int x = 0; x < acl.Count; x++)
{
    FileSystemAccessRule currentRule = (FileSystemAccessRule)acl[x];
    AccessControlType accessType = currentRule.AccessControlType;
    //Copy file cannot be executed for "List Folder/Read Data" and "Read extended attributes" denied permission
    if (accessType == AccessControlType.Deny && (currentRule.FileSystemRights & FileSystemRights.ListDirectory) == FileSystemRights.ListDirectory)
    {
        //we have deny copy - we can't copy the file
        denyEdit = true;
        break;
    }
... more checks 
}

Also, there are some strange cases where a certain right on the folder changes the right for the files regardless of their individual permissions (will see if I can find what it is).

Solution 2

Check article on code project which is about the thing you need, the is class created for this : The purpose of this class is to provide a simple answer to a common question, "Do I have permission to Read or Write this file?".

A simple way to test individual access rights for a given file and user

Note: cannot post whole code over here because its too long.

Solution 3

First, call Directory.GetFiles for root directory. Catch UnauthorizedAccessException - if none, you have full access.

If caught - call the function for each subdir recursively, catch the exception, if caught - add such dir to list.

Write a recursive function with external list for forbidden dirs

Share:
40,160
Stefan Steiger
Author by

Stefan Steiger

I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence &amp; Web Software Developer Technologies I work with: Microsoft Reporting- &amp; Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 &amp; 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML &amp; JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web &amp; DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).

Updated on July 09, 2022

Comments

  • Stefan Steiger
    Stefan Steiger almost 2 years

    I get an UnautorizedAccessException running this code:

    string[] fileList = Directory.GetFiles(strDir, strExt);
    

    The exception occurs in c:\users\username\appdata How can I check if I have access permission (to list and read files) ?

  • dahvyd
    dahvyd about 11 years
    That example is anything but simple.
  • dahvyd
    dahvyd over 10 years
    @Quandary, I'd tend to disagree in general, but that specific example does fifty bazillion things with terrible code, when the OP was asking about one very specific requirement. Overkill IMHO.
  • Thomas
    Thomas over 8 years
    It can certainly be simplified.