C# 设置权限中英对照表

使用C#根据不同的用户设置文件(夹)权限,

在C#中,权限的设置是求和集的一种机制,不会覆盖以前的权限;

设置 只读 权限

设置文件夹的某用户 只有读 权限 代码:

if (Directory.Exists(apath))
            {
                DirectoryInfo di = new DirectoryInfo(apath);
                DirectorySecurity dirSecurity = di.GetAccessControl();

                //删除存在的拒绝操作
                FileSystemAccessRule scanonly_one = new FileSystemAccessRule(username,
                     FileSystemRights.ChangePermissions | FileSystemRights.CreateDirectories | FileSystemRights.CreateFiles |
                     FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Modify | FileSystemRights.TakeOwnership |
                   FileSystemRights.Synchronize | FileSystemRights.Traverse, AccessControlType.Deny);
                dirSecurity.RemoveAccessRule(scanonly_one);
                //删除完全控制权限
                FileSystemAccessRule fullcontrol = new FileSystemAccessRule(username,
                   FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
               PropagationFlags.None, AccessControlType.Allow);
                dirSecurity.RemoveAccessRule(fullcontrol);

//设置权限规则二:禁止用户的写、删除、删除子文件夹及文件、更改权限、
//创建文件夹、创建文件、执行文件、更改文件(夹)所有者 等权限,
//且此权限规则 其子文件夹、文件都可以继承
                FileSystemAccessRule readOnlyRule_two = new FileSystemAccessRule(username,
                FileSystemRights.Write | FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles|
                FileSystemRights.ChangePermissions|FileSystemRights.CreateDirectories|FileSystemRights.CreateFiles|
                FileSystemRights.ExecuteFile|FileSystemRights.TakeOwnership,
                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                PropagationFlags.None, AccessControlType.Deny);

//设置权限规则一:给予用户此文件夹的读取、执行程序、列出文件夹、同步、等权限;
//且此权限规则 其子文件夹、文件都可以继承
                FileSystemAccessRule readOnlyRule_one = new FileSystemAccessRule(username,
                    FileSystemRights.Read | FileSystemRights.Traverse | FileSystemRights.ListDirectory | 
                    FileSystemRights.Synchronize,InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
               PropagationFlags.None, AccessControlType.Allow);

                dirSecurity.ResetAccessRule(readOnlyRule_one);
                dirSecurity.AddAccessRule(readOnlyRule_two);

                di.SetAccessControl(dirSecurity);

            }         

FileSystemRights 中子函数中英文对照

("AppendData", "附加数据");
("ChangePermissions", "更改权限");
("CreateDirectories", "创建文件夹/附加数据");
("CreateFiles", "创建文件/写入数据");
("Delete", "删除");
("DeleteSubdirectoriesAndFiles", "删除子文件夹及文件");
("ExecuteFile", "执行文件");
("FullControl", "完全控制");
("ListDirectory", "列出文件夹/读取数据");
("Modify", "修改");
("Read", "读取");
("ReadAndExecute", "读取和执行");
("ReadAttributes", "读取属性");
("ReadData", "读取数据");
("ReadExtendedAttributes", "读取扩展属性");
("ReadPermissions", "读取权限");
("Synchronize", "同步");
("TakeOwnership", "更改文件(夹)所有者");
("Traverse", "执行程序");
("Write", "写入");
("WriteAttributes", "写入属性");
("WriteData", "写入数据");
("WriteExtendedAttributes", "写入扩展属性");

继承规则组合

大家写这方面的代码是有米有发现:

在文件夹属性–高级里面,继承方式有6种选择,如下图:
这里写图片描述

而C#提供的这方面的参数只有:None 、InheritOnly 这2个,那么,怎么才可以实现上面的6中选择呢

答案就是 参数组合,如下图:
这里写图片描述

此处是翻译上图组合成C#代码:

private ApplyToType GetApplyToType(InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
{
    if (propagationFlags == PropagationFlags.None && 
        inheritanceFlags == InheritanceFlags.None) 
        return ApplyToType.ThisFolderOnly;

    if (propagationFlags == PropagationFlags.None && 
        inheritanceFlags == InheritanceFlags.ContainerInherit) 
        return ApplyToType.ThisFolderAndSubfolders;

    if (propagationFlags == PropagationFlags.None && 
        inheritanceFlags == InheritanceFlags.ObjectInherit) 
        return ApplyToType.ThisFolderAndFiles;

    if (propagationFlags == PropagationFlags.None && 
        inheritanceFlags == (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit)) 
        return ApplyToType.ThisFolderSubfoldersAndFiles;

    if (propagationFlags == PropagationFlags.InheritOnly && 
        inheritanceFlags == InheritanceFlags.ContainerInherit)
        return ApplyToType.SubfoldersOnly;

    if (propagationFlags == PropagationFlags.InheritOnly && 
        inheritanceFlags == InheritanceFlags.ObjectInherit)
        return ApplyToType.FilesOnly;

    if (propagationFlags == PropagationFlags.InheritOnly && 
        inheritanceFlags == (InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit))
        return ApplyToType.SubfoldersAndFilesOnly;

    return ApplyToType.AndreDoesntKnow;
}

希望能帮到大家~~

本文出自he ivy 的博客,转载请注明出处:http://blog.csdn.net/heivy/article/details/53518156

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值