Reputation: 95
I need check if a specific user (Domain or Local), has mentioned rights (Read / Write) on the given directory.
The method should return true even the User is inheriting the rights from User Group (like Administrators).
This answer works fine but it is limited to Current User only
Upvotes: 0
Views: 5452
Reputation: 133
Try the bellow function
using System.IO;
using System.Security.AccessControl;
public static bool CheckWritePermissionOnDir(string path)
{
var writeAllow = false;
var writeDeny = false;
var accessControlList = Directory.GetAccessControl(path); Control
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;
}
Upvotes: 1