c# - 检查 C# 中的访问权限 - "on Linux"

标签 c# linux permissions

我想知道如何在使用 C# 时检查 Linux 系统上文件和目录的访问权限。

我想知道我是否可以读、写或执行。

System.IO.Directory.GetAccessControl() 在 Linux 上不工作。

未处理的异常: System.PlatformNotSupportedExcpetion:此平台不支持操作。

跟踪开始于:System.Security.AccessControl.NativeObjectSecurity.InternalGet

我尝试使用的方法:

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);
}

文件路径的方法几乎相同。

最佳答案

我自己想出了一个有点“肮脏”的解决方案。

  • 我使用重定向输出启动 bash
  • 传递 ls -l + 文件或目录的路径作为参数
  • 在末尾找到文件名或目录名的行
  • 最后我检查该行的第 2/3/4 个字符是否为 r/w/x

https://pastebin.com/1PQNaB1m

关于c# - 检查 C# 中的访问权限 - "on Linux",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49430088/

相关文章:

Swift 在 Ubuntu : Handling whitespace in object file names 上构建

c# - 如何将UpdateSourceTrigger的值交给UserControl或者运行时更新?

c# - 使用正则表达式匹配递增/递减序列

javascript - UpdatePanel 内的 ASP.NET GridView 直到第二次单击才会更新

c# - WSS3 - 创建后在 SPFieldType.Boolean 上设置默认值

c - 在 linux 中编译的以下 c 代码中的段错误

linux - Zynq-7000可以单步吗

androidx requestPermissionLauncher 导致 java.lang.IllegalArgumentException : Can only use lower 16 bits for requestCode

c# - 图形 API - 权限不足,无法完成操作

java - 在tomcat7上运行shell脚本