C#修改文件权限

用户名的格式为:Local MachineName\AccountName

机器名可通过System.Environment.MachineName获取。

 

获取一个文件的权限(帐号)列表

FileSecurity fsec = new FileInfo(path).GetAccessControl();
AuthorizationRuleCollection ar= fsec.GetAccessRules(truetruetypeof(System.Security.Principal.NTAccount));
foreach (AuthorizationRule r in ar)
    r.IdentityReference.Value;


给一个文件赋于IIS帐号的写权限

FileSecurity fsec = new FileInfo(path).GetAccessControl();
fsec.SetAccessRule(new FileSystemAccessRule(@"WEB-01\IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));

 

设置文件夹的权限

DirectoryInfo dinfo = new DirectoryInfo(path);
DirectorySecurity dsecurity = dinfo.GetAccessControl();
dsecurity.AddAccessRule(new FileSystemAccessRule(@"WEB-01\IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));

 

posted @ 2013-09-11 11:24  chy710  阅读(770)  评论(0编辑  收藏  举报