title: How to set folder permission
body: <p>How can I create/edit/add folder permission to specific folder? There is a folder called "test" in local disk C. How do I set permission to that folder using C#?</p>
  
  <p>I wrote some code already:</p>
  
  <pre><code>public void getusers()
  {
      SelectQuery squery = new SelectQuery("Win32_UserAccount", "Domain='" + System.Environment.UserDomainName.ToString() + "'");
      try
      {
          ManagementObjectSearcher msearchar = new ManagementObjectSearcher(squery);
  
          foreach (ManagementObject mobject in msearchar.Get())
          {
              comboBox1.Items.Add(mobject["Name"]);
          }
      }
      catch (Exception e) { MessageBox.Show(e.ToString()); }
  }
  
  private void button1_Click(object sender, EventArgs e)
  {
      FolderBrowserDialog fbd = new FolderBrowserDialog();
      fbd.ShowDialog();
      textBox1.Text = fbd.SelectedPath.ToString();
  }
  
  private void button2_Click(object sender, EventArgs e)
  {
      DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);
  
      DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
      string User = System.Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
  
      myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
      //myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
  
      myDirectoryInfo.SetAccessControl(myDirectorySecurity);
      MessageBox.Show("Permissions Altered Successfully" + User);
  }
  </code></pre>
  
  <p>This code already successfully adds the User to the folder but the permissionIi set to that folder is not inherited at all. Did I miss something? Or could someone guide me how to inherit the permission to that folder?</p>