您的位置:首页 > 编程语言 > C#

C# 设置权限中英对照表

2016-12-08 14:49 381 查看
使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息