C#修改文件权限
用户名的格式为:Local MachineName\AccountName
机器名可通过System.Environment.MachineName获取。
获取一个文件的权限(帐号)列表
FileSecurity fsec = new FileInfo(path).GetAccessControl();
AuthorizationRuleCollection ar= fsec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (AuthorizationRule r in ar)
r.IdentityReference.Value;
AuthorizationRuleCollection ar= fsec.GetAccessRules(true, true, typeof(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));
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));
DirectorySecurity dsecurity = dinfo.GetAccessControl();
dsecurity.AddAccessRule(new FileSystemAccessRule(@"WEB-01\IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));