0

I am trying to set ntfs permissions for a user folder such that the user has Read and Execute permissions for This Folder Only but has Full Control for All Subfolders and Files.
I am using this code, which is not doing what I need:

public void AddDirectorySecurity(string dirName, string userName)
{
    DirectoryInfo dInfo = new DirectoryInfo(dirName);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();

    dSecurity.AddAccessRule(new FileSystemAccessRule(userName,
     FileSystemRights.ReadAndExecute, InheritanceFlags.ObjectInherit,
     PropagationFlags.InheritOnly, AccessControlType.Allow));

    dInfo.SetAccessControl(dSecurity);

    dSecurity.AddAccessRule(new FileSystemAccessRule(userName,
     FileSystemRights.FullControl, InheritanceFlags.None,
     PropagationFlags.None, AccessControlType.Allow));  

    dInfo.SetAccessControl(dSecurity);
}

Can someone please offer some advice on what I need to change to get this working correctly?

2
Contributors
1
Reply
7
Views
3 Years
Discussion Span
Last Post by pitic
0

After you set the ReadAndExecute permission on the parent folder do a loop through all the directories inside that folder and set their appropriate permissions

foreach (var folder in dInfo.GetDirectories())
{
    var security = folder.GetAccessControl();
    security.AddAccessRule(...);
    folder.SetAccessControl(security);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.