Monday, May 14, 2012

Programmatically assign access control list (ACL) permission to 'this folder, subfolders and files'

I have to assign permission on a folder and it's child folder and files programmatically using C#.NET. I'm doing this as below:



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)
{
//...
}


It does assign permission on my 'C:\ROOT' folder. But it assign permission to the Subfolders and Files only but not the 'ROOT' folder.
enter image description here



Q: How can I define the FileSystemAccessRule instance to assign permission to the ROOT folder, Subfolders and files?





No comments:

Post a Comment