Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取文件夹';s权限(短版本)_C#_.net 3.5 - Fatal编程技术网

C# 获取文件夹';s权限(短版本)

C# 获取文件夹';s权限(短版本),c#,.net-3.5,C#,.net 3.5,我使用以下代码返回详细的文件夹权限 return (from permissionName in Enum.GetNames(typeof(FileSystemRights)) let val = Convert.ToInt32( Enum.Parse(typeof(FileSystemRights), permissionName), CultureInfo.InvariantCulture)

我使用以下代码返回详细的文件夹权限

return (from permissionName in Enum.GetNames(typeof(FileSystemRights))
            let val =
                Convert.ToInt32(
                    Enum.Parse(typeof(FileSystemRights), permissionName), CultureInfo.InvariantCulture)
            where
                (val != 0x1f01ff) && (val != 0x301bf) && (val != 0x20089) && (val != 0x200a9) && (val != 0x116)
            where ((int)this.FileSystemRule.FileSystemRights & val) > 0
            select permissionName).ToList();

如何获取简短版本,即:

换句话说,我只想回来

  • 完全控制
  • 修改
  • 读取并执行
  • 列出文件夹内容
  • 阅读
  • 特殊(适用于任何其他组合)

  • 只要列出所需的权限,就可以大大简化逻辑。例如:

    private readonly FileSystemRights[] namesIWant = new FileSystemRights[]
    {
        FileSystemRights.FullControl, 
        FileSystemRights.Modify, 
        FileSystemRights.ReadAndExecute,
        FileSystemRights.ListDirectory,
        FileSystemRights.Read,
        FileSystemRights.Write
    };
    
    然后,LINQ表达式变为:

    return (from perm in namesIWant
            where (this.FileSystemRule.FileSystemRights & perm) != 0
            select perm.ToString()).ToList();
    

    我想和你分享一下, 为了让它适合我,我不得不将
    单音改为
    =
    @Jim Mischel的代码修改为:

    return (from perm in namesIWant
            where (this.FileSystemRule.FileSystemRights & perm) == perm 
            select perm.ToString()).ToList();
    

    同时从
    NamesIWant
    中删除
    FileSystemRights.ListDirectory,
    ,因为它包括
    Read
    ReadData
    ,如果保留它,您将拥有重复的值。

    您应该使用
    !=0
    而不是
    >0
    或强制转换为
    uint
    。当有人扩展代码并检查位31(=GenericRead)时,结果是<0。