views:

1414

answers:

1

Hi Guys,

I've been having problems programatically assigning permissions to Folders / Registry entries. I have managed to assign inheriting permissions using the following code:

FileSystemAccessRule rule = new FileSystemAccessRule(LOGON_USER_NAME, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);

DirectorySecurity security = new DirectorySecurity(); 
security.SetAccessRule(rule);

Directory.CreateDirectory(dir);
Directory.SetAccessControl(dir, security);

This correctly sets my file permissions on all the child folders i create as an administrator. However, it does not set the permissions on the 'dir' folder itself. I've played around with a fair few permutations for inheritance and propogation, but not had any joy.

For example - i have:

dir = %programfiles%\Test

If i have created a folder in test (%programfiles%\Test\SubFolder), i have full permissions assigned to it for my user, but i do not have full permissions on %programfiles%\Test. This is really annoying, as i would like to give my user full permissions to do whatever with the Test directory as well.

I am having similar problems with registry permissions, but i believe that if i can solve one, i can solve both of the outstanding issues.

Does anyone know how this can be resolved?

Regards

Tris

+3  A: 

FileSystemAccessRule rule = new FileSystemAccessRule(LOGON_USER_NAME, FileSystemRights.FullControl, AccessControlType.Allow); <-- is correct for the folder

FileSystemAccessRule rule = new FileSystemAccessRule(LOGON_USER_NAME, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow); <-- is correct for subfolders and files;

both lines need to be in your project. then you get acls that apply to this folder, subfolders and files

Dave
nice one, thanks!
Run CMD