C# – Setting Folder permissions on Vista

c++file-permissionssecurity

I am trying to set the permissions of a folder and all of it's children on a vista computer. The code I have so far is this.

 public static void SetPermissions(string dir)
        {
            DirectoryInfo info = new DirectoryInfo(dir);
            DirectorySecurity ds = info.GetAccessControl();            
            ds.AddAccessRule(new FileSystemAccessRule(@"BUILTIN\Users", 
                             FileSystemRights.FullControl, 
                             InheritanceFlags.ContainerInherit,
                             PropagationFlags.None, 
                             AccessControlType.Allow));

            info.SetAccessControl(ds);            
        }

However it's not working as I would expect it to.
Even if I run the code as administrator it will not set the permissions.

The folder I am working with is located in C:\ProgramData\<my folder> and I can manually change the rights on it just fine.

Any one want to point me in the right direction.

Best Solution

So the answer is two fold. First off a sub folder was being created before the permissions were set on the folder and I needed to or in one more flag on the permissions to make it so both folders and files inherited the permissions.

public static void SetPermissions(string dir)
        {
            DirectoryInfo info = new DirectoryInfo(dir);
            DirectorySecurity ds = info.GetAccessControl();            
            ds.AddAccessRule(new FileSystemAccessRule(@"BUILTIN\Users", 
                             FileSystemRights.FullControl,
                             InheritanceFlags.ObjectInherit |
                             InheritanceFlags.ContainerInherit,
                             PropagationFlags.None,
                             AccessControlType.Allow));
            info.SetAccessControl(ds);            
        }

After that every thing appears to be working.