C#修改文件权限

用户名的格式为:Local MachineNameAccountName

机器名可通过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-01IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));

设置文件夹的权限

DirectoryInfo dinfo = new DirectoryInfo(path);
DirectorySecurity dsecurity = dinfo.GetAccessControl();
dsecurity.AddAccessRule(new FileSystemAccessRule(@"WEB-01IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));
原文地址:https://www.cnblogs.com/chy710/p/file_access.html