I would like to know how to check the access permission for files and directories on a Linux system while using C#.
I want to know if i can read, write or excecute.
System.IO.Directory.GetAccessControl()
does not work on Linux.
Unhandled Exception:
System.PlatformNotSupportedExcpetion: Operation is not supported on this platform.
trace starts at: System.Security.AccessControl.NativeObjectSecurity.InternalGet
The Method I tried to use:
static bool HasDirectoryPermission(FileSystemRights right, string path) {
var allowed = false;
var denied = false;
var accessControlList = System.IO.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 ((right & rule.FileSystemRights) != right)
continue;
if (rule.AccessControlType == AccessControlType.Allow)
allowed = true;
else if (rule.AccessControlType == AccessControlType.Deny)
denied = true;
}
return (allowed && !denied);
}
The Method for file paths is pretty much the same.