C# - Windows ACL - Applying Inherited Permissions C# - Windows ACL - Applying Inherited Permissions windows windows

C# - Windows ACL - Applying Inherited Permissions


For the folder:

FileSystemAccessRule rule = new FileSystemAccessRule(LOGON_USER_NAME,     FileSystemRights.FullControl, AccessControlType.Allow);

For subfolders and files:

FileSystemAccessRule rule = new FileSystemAccessRule(LOGON_USER_NAME,    FileSystemRights.FullControl, InheritanceFlags.ContainerInherit |      InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly,     AccessControlType.Allow);

both lines need to be in your project. then you get acls that apply to this folder, subfolders and files


I'm hardly an expert here, but after having to figure this out for my own purposes, I believe that Dave's answer, although functional, is overly complicated. You should be able to achieve this with just one rule:

FileSystemAccessRule rule = new FileSystemAccessRule(LOGON_USER_NAME,    FileSystemRights.FullControl,    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,    PropagationFlags.None,     AccessControlType.Allow);

The PropagationFlags.InheritOnly parameter used by the OP in their original code is what prevents the access rule from applying to the object itself.

Also, you might as well set the directory's security as you're creating it, since .NET provides an overload for just that purpose:

Directory.CreateDirectory(dir, security);