Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add "Everyone" privilege to folder using C#.NET

I have used the code below to allow Everyone access to a folder:

System.Security.AccessControl.DirectorySecurity sec =     System.IO.Directory.GetAccessControl(directory, AccessControlSections.All); FileSystemAccessRule accRule = new FileSystemAccessRule("Everyone",                                        FileSystemRights.Modify,                                        AccessControlType.Allow); sec.AddAccessRule(accRule);    // setACL sec.ResetAccessRule(accRule); 

Now, the Everyone user is added to the folder, but not with any rights assigned. All the read, write, execute etc. checkboxes are not checked.

like image 537
Suresh Chaudhary Avatar asked Mar 14 '11 13:03

Suresh Chaudhary


People also ask

How do I give permission to a folder for everyone?

Right-click the folder, and then click Properties. Click the Sharing tab, and then click Share to open the File Sharing dialog box. In the File Sharing dialog box, type Everyone in the box, and then click Add. By default, Read permissions are applied for the Everyone object.

How can I give permission to a folder in C#?

To grant directory rights that inherit "normally" (i.e. propagate to subdirectories and all files), use the following values for InheritanceFlags and PropagationFlags: InheritanceFlags. ContainerInherit | InheritanceFlags. ObjectInherit and PropagationFlags. None .

How do I give a group access to a folder?

chmod o-rwx foldername To change directory permissions for everyone, use “u” for users, “g” for group, “o” for others, and “ugo” or “a” (for all). chmod ugo+rwx foldername to give read, write, and execute to everyone. chmod a=r foldername to give only read permission for everyone.


1 Answers

First thing I want to tell you is how I found this solution. This is probably more important than the answer because file permissions are hard to get correct.

First thing I did was set the permissions I wanted using the Windows dialogs and checkboxes. I added a rule for "Everyone" and ticked all boxes except "Full Control".

Then I wrote this C# code to tell me exactly what parameters I need to duplicate the Windows settings:

string path = @"C:\Users\you\Desktop\perms"; // path to directory whose settings you have already correctly configured DirectorySecurity sec = Directory.GetAccessControl(path); foreach (FileSystemAccessRule acr in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) {     Console.WriteLine("{0} | {1} | {2} | {3} | {4}", acr.IdentityReference.Value, acr.FileSystemRights, acr.InheritanceFlags, acr.PropagationFlags, acr.AccessControlType); } 

This gave me this line of output:

Everyone | Modify, Synchronize | ContainerInherit, ObjectInherit | None | Allow 

So the solution is simple (yet hard to get right if you don't know what to look for!):

DirectorySecurity sec = Directory.GetAccessControl(path); // Using this instead of the "Everyone" string means we work on non-English systems. SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); Directory.SetAccessControl(path, sec); 

This will make the checkboxes on the Windows security dialog match what you have already set for your test directory.

like image 136
Yoshi Avatar answered Sep 24 '22 00:09

Yoshi