Search code examples
c#aclntfs

Cannot set Full Control permission for folder


I'm trying to add the Full Control permission (for a NT service account) to a folder through C#. However, the permission is not set, what I am missing here?

var directoryInfo = new DirectoryInfo(@"C:\Test");
var directorySecurity = directoryInfo.GetAccessControl();

directorySecurity.AddAccessRule(new FileSystemAccessRule("NT Service\\FileMoverService",
    FileSystemRights.FullControl, AccessControlType.Allow));

directoryInfo.SetAccessControl(directorySecurity);

folder permissions


Solution

  • You need to specify the inheritance flags:

    directorySecurity.AddAccessRule(new FileSystemAccessRule(@"NT Service\FileMoverService",
        FileSystemRights.FullControl,
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
        PropagationFlags.None,
        AccessControlType.Allow));