以编程方式将访问控制列表(ACL)权限分配给“此文件夹,子文件夹和文件”

我必须分配一个文件夹的权限,它是使用C#.NET编程的子文件夹和文件。 我这样做如下:

var rootDic = @"C:\ROOT"; var identity = "NETWORK SERVICE"; //The name of a user account. try { var accessRule = new FileSystemAccessRule(identity, fileSystemRights: FileSystemRights.Modify, inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, propagationFlags: PropagationFlags.InheritOnly, type: AccessControlType.Allow); var directoryInfo = new DirectoryInfo(rootDic); // Get a DirectorySecurity object that represents the current security settings. DirectorySecurity dSecurity = directoryInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. dSecurity.AddAccessRule(accessRule); // Set the new access settings. directoryInfo.SetAccessControl(dSecurity); } catch (Exception ex) { //... } 

它确实分配了我的'C:\ ROOT'文件夹的权限。 但是它仅将权限分配给子文件夹和文件,而不是“ROOT”文件夹。 在这里输入图像说明

问:如何定义FileSystemAccessRule实例以将权限分配给ROOT文件夹,子文件夹和文件?

Solutions Collecting From Web of "以编程方式将访问控制列表(ACL)权限分配给“此文件夹,子文件夹和文件”"