C# 设置权限中英对照表
2016-12-08 14:49
381 查看
使用C#根据不同的用户设置文件(夹)权限,
在C#中,权限的设置是求和集的一种机制,不会覆盖以前的权限;
在文件夹属性–高级里面,继承方式有6种选择,如下图:
而C#提供的这方面的参数只有:None 、InheritOnly 这2个,那么,怎么才可以实现上面的6中选择呢
答案就是 参数组合,如下图:
此处是翻译上图组合成C#代码:
希望能帮到大家~~
本文出自he ivy 的博客,转载请注明出处:http://blog.csdn.net/heivy/article/details/53518156
在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
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#使用第三方组件生成二维码汇总
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#网络适配器简单操作
- C#实现导出List数据到xml文件的方法【附demo源码下载】
- C#异步绑定数据实现方法