Changing permissions on child folders in C#

后端 未结 2 1669
孤城傲影
孤城傲影 2021-01-19 23:54

I\'m writing a DLL to change permissions on a folder and everything underneath the folder. Below is the code that I have right now.

The problem comes when I call ad

相关标签:
2条回答
  • 2021-01-20 00:16

    You have to do it recursively. You can specify inheritance rules for new folders/files but for existing you have to do it yourself.

    0 讨论(0)
  • 2021-01-20 00:17

    This question is old but I was looking for the same thing and found a solution:

    var dirInfo = new DirectoryInfo(dirName);
    var dirSecurity = dirInfo.GetAccessControl();
    
    // Add the DirectorySystemAccessRule to the security settings. 
    dirSecurity.AddAccessRule(new FileSystemAccessRule(
        account, 
        rights, 
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
        PropagationFlags.None, 
        AccessControlType.Allow));
    
    // Set the new access settings.
    dirInfo.SetAccessControl(dirSecurity);
    

    greetings

    0 讨论(0)
提交回复
热议问题