I have a widows folder on a remote server from which I want to remove permissions for a specific user. I tried many methods and nothing works.
I am not getting errors with the following code, but the resolution remains intact. Am I not using the correct objects or missing a step with these objects? Any help would be greatly appreciated.
DirName is passed as a share, e.g.. \ MyServer \ my_folder
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); } } }
source share