Remove all access rules from a directory
A while ago i needed to write some code that removes all (existing/inherited) access rules from a given directory. It was pretty frustrating to notice that all my attempts seemed to fail (RemoveAccessRule, PurgeAccessRule, …)
Finally i found that SetAccessRuleProtection was the method that i needed to invoke.
const string Folder = @"c:\temp\secured";
var directory = new DirectoryInfo(Folder);
var directorySecurity = directory.GetAccessControl();
directorySecurity.SetAccessRuleProtection(true,false);
directory.SetAccessControl(directorySecurity);
There you go 😉