Recursive security settings

I would like to apply folder security settings for all descendants in C #. Essentially, I would like to do the same thing as "Replace all existing inherited permissions for all descendants with inherited permissions from this object" in "Advanced Security Settings for [Folder]".

Are there any elegant ways to get closer to this?

+3
source share
2 answers

After some quality time with Google and MSDN, I came up with the following bit of code. Everything seems to be working fine.

static void Main(string[] args) { DirectoryInfo dInfo = new DirectoryInfo(@"C:\Test\Folder"); DirectorySecurity dSecurity = dInfo.GetAccessControl(); ReplaceAllDescendantPermissionsFromObject(dInfo, dSecurity); } static void ReplaceAllDescendantPermissionsFromObject( DirectoryInfo dInfo, DirectorySecurity dSecurity) { // Copy the DirectorySecurity to the current directory dInfo.SetAccessControl(dSecurity); foreach (FileInfo fi in dInfo.GetFiles()) { // Get the file FileSecurity var ac = fi.GetAccessControl(); // inherit from the directory ac.SetAccessRuleProtection(false, false); // apply change fi.SetAccessControl(ac); } // Recurse into Directories dInfo.GetDirectories().ToList() .ForEach(d => ReplaceAllDescendantPermissionsFromObject(d, dSecurity)); } 
+3
source

You can find the DirectorySecurity class that will be useful for this.

http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx

System.Security.AccessControl may have some other valuable tools. Namespace

+2
source

Source: https://habr.com/ru/post/1383192/


All Articles