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.

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);



Leave a Reply