Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# – How to create a directory in which only Administrator has full control?

I’ve created in C# a program which create a directory and some files inside it.

How can I set (programmatically – if possible) the Full-Control right only for Administrator? The ideal case would be to inhibit the Write and Read rights for a non-administrator user (creating a "kind of hidden folder"), but it would also be enough for me to set only the Read right for a non-administrator user, stop.

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please, I’ve tried to search in StackOverflow, but nobody has wrote the same problem. Infact, everybody talks about "all users". Me no.

>Solution :

Yup, it’s possible. First, you have to create the directory then set it’s access rules.

var directory = Directory.CreateDirectory("SomeFolder");

var directorySecurity = directory.GetAccessControl();

var administratorRule = new FileSystemAccessRule("Administrators", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
var usersRole = new FileSystemAccessRule("Users", FileSystemRights.CreateDirectories | FileSystemRights.CreateFiles, AccessControlType.Allow);

directorySecurity.AddAccessRule(administratorRule);
directorySecurity.AddAccessRule(usersRole);

directory.SetAccessControl(directorySecurity);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading