JimDel
JimDel

Reputation: 4359

Changing Folder permissions with C# doesn't seem to be working

I'm really out of my comfort zone when it comes to permisions. But I want to create a folder and give all users Full Control over it.

DirectoryInfo NewDir = Directory.CreateDirectory(@"C:\Test");
DirectorySecurity dSecur = NewDir.GetAccessControl();
FileSystemAccessRule fAccess =
    new FileSystemAccessRule("Users", FileSystemRights.FullControl,AccessControlType.Allow);
dSecur.AddAccessRule(fAccess);
NewDir.SetAccessControl(dSecur);

But the pic below shows that Users Still doesn't have Full Control.

enter image description here

Am I missing something? Thanks!

Upvotes: 0

Views: 115

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

If you want full control then you need to pass both ContainerInherit and ObjectInherit for the InheritanceFlags.

new FileSystemAccessRule(
    User, 
    FileSystemRights.FullControl, 
    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
    PropagationFlags.InheritOnly, 
    AccessControlType.Allow
)

And be sure that run under a user with enough rights to grant full control the other user.

Upvotes: 1

Related Questions