I would like to use the version of Directory.CreateDirectory() that
takes security info:
Directory.CreateDirectory(String, DirectorySecurity)
(I don't want to apply security afterwards (I know how to do that) ---
I want it straightaway)
Thing is a I am not sure how to "make up" a DirectorySecurity instance
to pass, and I can't find any examples.
Hi,
Setting file security access is pretty straightforward. You need to create
a set of FileSystemAccessRules and add them to a DirectorySecurity object
which you in turn use in the Directory creation. The code below creates two
access rules. Administrators gets full control for the current folder and
all subfolders as well as for all files within that folder and within any
subfolder. Guest is given folder creation rights, but only to create
subfolders or files within GuestTemp.
FileSystemAccessRule administratorRule = new FileSystemAccessRule(
"Administrators",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
FileSystemAccessRule guestRule = new FileSystemAccessRule(
"Guest",
FileSystemRights.CreateDirectories | FileSystemRights.CreateFiles,
AccessControlType.Allow);
DirectorySecurity dirSec = new DirectorySecurity();
dirSec.AddAccessRule(administratorRule);
dirSec.AddAccessRule(guestRule);
Directory.CreateDirectory(@"C:\GuestTemp", dirSec);
To achieve a full check in the security tab you need to set a combinationof
rights. To see which rights are necessary to achieve this you can use the
[Advanced] button in the security tab. Select a user and then the [Edit]
button to see the exact rights granted.
You may also want to start off with an existing DirectorySecurity by using
Directory.GetAccessControl(...);
Tweak the flags and see the result in the security tab.
--
Happy Coding!
Morten Wennevik [C# MVP]- Hide quoted text -
- Show quoted text -