我试图progamrtaiclly允许写入目录上的ASPNET帐户.我使用以下代码执行此操作:(请注意,我希望ASPNET的"允许写入访问"也传播到子对象:
static void Main(string[] args) { FileSecurity fileSecurity; fileSecurity = new FileSecurity(); fileSecurity.SetAccessRuleProtection(true, false); fileSecurity.AddAccessRule(new FileSystemAccessRule("ASPNET",FileSystemRights.Write,InheritanceFlags.ObjectInherit|InheritanceFlags.ContainerInherit,PropagationFlags.InheritOnly,AccessControlType.Allow)); File.SetAccessControl("C:\\TestDir1", fileSecurity); }
此代码导致异常:"无法设置标志.\ r \nParameter name:inheritanceFlags"
可能有什么不对?
得到了解决方案,显然我必须这样做:
DirectoryInfo dirInfo = new DirectoryInfo("C:\\TestDir2"); DirectorySecurity dirSecurity = dirInfo.GetAccessControl(); dirSecurity.AddAccessRule(new FileSystemAccessRule("ASPNET", FileSystemRights.Write|FileSystemRights.DeleteSubdirectoriesAndFiles, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)); dirInfo.SetAccessControl(dirSecurity);