Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set full control to a directory?

Tags:

c#

.net

I am using the following simple code to add full control to a directory, but it doesn't work.

        String dir_name = @"folder_full_path";
        DirectorySecurity dir_security = Directory.GetAccessControl(dir_name);
        FileSystemAccessRule access_rule = new FileSystemAccessRule(@"AccountName",
            FileSystemRights.FullControl, AccessControlType.Allow);
        dSecurity.AddAccessRule(access_rule);
        Directory.SetAccessControl(dir_name, dir_security);

But this code only set special permissions to the target folder. This code is almost the same as the MSDN sample. I am scratching my head for a reasonable explanation... Hope someone could shed some light on me.

Many thanks.

like image 968
smwikipedia Avatar asked May 09 '10 15:05

smwikipedia


People also ask

How do I set 777 permissions to a folder in Windows 10?

Easiest way to set permissions to 777 is to connect to Your server through FTP Application like FileZilla, right click on folder, module_installation, and click Change Permissions - then write 777 or check all permissions.


1 Answers

After some reverse Engineering of the original ACL rules I got it to work with the following code:

IdentityReference everybodyIdentity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

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

May it helps any further visitor :)

like image 163
GameScripting Avatar answered Nov 02 '22 23:11

GameScripting