title: Remove permissions from a windows folder
body: <p>I have a widows folder on a remote server from which I wish to remove permissions for a specific user.  I've tried numerous methods and nothing seems to work.  </p>
  
  <p>I get no errors with the following code but the permission remains intact. Am I not using the correct objects or missing some step with these objects?  Any assistance would be greatly appreciated.  </p>
  
  <p>The dirName is passed as a share, e.g. \myserver\myfolder</p>
  
  <pre><code>private void removePermissions(string dirName, string username)
      {
          string user = System.Environment.UserDomainName + "\\" + username;
          DirectoryInfo dirinfo = new DirectoryInfo(dirName);
          DirectorySecurity dsec = dirinfo.GetAccessControl(AccessControlSections.All);
  
          AuthorizationRuleCollection rules = dsec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
          foreach (AccessRule rule in rules)
          {
              if (rule.IdentityReference.Value == user)
              {
                  bool value;
                  dsec.PurgeAccessRules(rule.IdentityReference);
                  dsec.ModifyAccessRule(AccessControlModification.RemoveAll, rule, out value);
                  MessageBox.Show("Removed permission from " + dirName + " for " + user);
              }
          }
      }
  </code></pre>