.Net下查看和修改文件夹的ACL安全权限(C#)
来源:互联网 发布:新手网络作家怎么赚钱 编辑:程序博客网 时间:2024/05/26 09:57
这是一个在.Net下修改文件夹或文件的ACL安全权限的类:
SetFolderACL:两个重载函数,设置权限的方法,根据需要选择重载。
GetACL: 查看文件夹权限的信息,用户名-权限键值对
GetACLString:查看文件夹权限的文本信息,用户名-权限名键值对
using System;using System.Collections;using System.Text;using System.Security.AccessControl;using System.IO;
namespace ACL{ class ACL_FS { //By 同济黄正 http://hz932.ys168.com public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny) { InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; return SetFolderACL(FolderPath , UserName , Rights , AllowOrDeny , inherits , PropagationFlags.None , AccessControlModification.Add); }
public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny , InheritanceFlags Inherits , PropagationFlags PropagateToChildren , AccessControlModification AddResetOrRemove) { //过程:获取文件夹安全对象、构造访问规则、修改安全对象的访问规则、重新设置文件夹安全对象 bool ret; DirectoryInfo folder = new DirectoryInfo(FolderPath); DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All); FileSystemAccessRule accRule = new FileSystemAccessRule(UserName , Rights , Inherits , PropagateToChildren , AllowOrDeny); dSecurity.ModifyAccessRule(AddResetOrRemove , accRule , out ret); folder.SetAccessControl(dSecurity); return ret; }
/// <returns>String,FileSystemRights键值对</returns> public static Hashtable GetACL(String FolderPath) { Hashtable ret = new Hashtable(); DirectorySecurity sec = Directory.GetAccessControl(FolderPath , AccessControlSections.All); foreach (FileSystemAccessRule rule in sec.GetAccessRules(true , true , typeof(System.Security.Principal.NTAccount))) { ret[rule.IdentityReference.ToString()] = rule.FileSystemRights; } return ret; } public static string GetACLString(String FolderPath) { StringBuilder sb = new StringBuilder(); Hashtable rights=GetACL(FolderPath); foreach (string key in rights.Keys) { sb.Append(key + ":/t" + ((FileSystemRights)rights[key]).ToString()+"/r/n"); } return sb.ToString(); }
}}//以上在WindowsXP、Windows Server 2003下测试通过。
- .Net下查看和修改文件夹的ACL安全权限(C#)
- .Net下查看和修改文件夹的ACL安全权限(C#)
- .Net下修改文件夹或文件的ACL安全权限
- ACL权限的查看和设定
- Linux 文件和文件夹的权限新建、查看和修改
- android下创建文件夹和修改其权限的方法
- Linux下目录文件权限的查看和修改
- ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- Ubuntu下修改文件夹权限
- ubuntu下修改文件夹权限
- Linux文件权限的查看和修改
- Linux下查看文件权限、修改文件权限的方法
- Dreamweaver 批量替换注释
- BeanUtils的copyProperties方法不能复制非public声明类的属性
- 关于vector删除操作的一点提示。
- 让单元测试“嗅出”代码的臭味
- em db control错误
- .Net下查看和修改文件夹的ACL安全权限(C#)
- 判断单链表是否有环
- 实现GridView表头固定及添加表头背景
- 122
- 明天又是一个挑战自我的一次好机会哦!!~~
- C语言中的函数指针
- asp.net里事件的触发顺序
- 《魔兽世界》术士最新可用宏大汇总 2.4版
- 数字电视及其标准