Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 957
Kibria Avatar asked May 14 '12 05:05

Kibria


1 Answers

You just need to remove the PropagationFlags.InheritOnly flag. By specifying that you are stating that the ACE should not apply to the the target folder. Use PropagationFlags.None instead. You may find this MSDN article helpful.

like image 177
David Heffernan Avatar answered Oct 10 '22 15:10

David Heffernan