title: Changing permissions on child folders in C#
body: <p>I'm writing a DLL to change permissions on a folder and everything underneath the folder. Below is the code that I have right now. </p>
  
  <p>The problem comes when I call addPermissions(). It's correctly setting the permissions on the dirName folder and any folder that I later create under dirName, but any folder that exists when I add permissions doesn't get the additional permissions. </p>
  
  <p>Do I need to recursively set the permissions on all child folders? Or is there a way to do this with a line or two of code?</p>
  
  <pre><code>public class Permissions
  {
      public void addPermissions(string dirName, string username)
      {
          changePermissions(dirName, username, AccessControlType.Allow);
      }
  
      public void revokePermissions(string dirName, string username)
      {
          changePermissions(dirName, username, AccessControlType.Deny);
      }
  
      private void changePermissions(string dirName, string username, AccessControlType newPermission)
      {
          DirectoryInfo myDirectoryInfo = new DirectoryInfo(dirName);
  
          DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
  
          string user = System.Environment.UserDomainName + "\\" + username;
  
          myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(
              user, 
              FileSystemRights.Read | FileSystemRights.Write | FileSystemRights.ExecuteFile | FileSystemRights.Delete, 
              InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
              PropagationFlags.InheritOnly, 
              newPermission
          ));
  
          myDirectoryInfo.SetAccessControl(myDirectorySecurity);
      }
  }
  </code></pre>