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?

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.