Checking file/folder access permission
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
Stefan Steiger
I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence & Web Software Developer Technologies I work with: Microsoft Reporting- & 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 & 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML & JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web & 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, 2022Comments
-
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 about 11 yearsThat example is anything but simple.
-
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 over 8 yearsIt can certainly be simplified.