我试图创build新的目录并以编程方式设置权限。 我已经能够创build目录,为单个用户和组应用权限,并更新大多数inheritance和传播设置。 但是,我无法使用代码来检查在窗口GUI界面(见下面)中find的包含此对象的父项的可inheritance权限 。
我试着使用替代值的inheritance和Propagation标志,以及SetAccessRuleProtection
参数,但我似乎无法find任何排列的作品。
代码示例:
using System.Security.AccessControl; //... string dir = @"C:\MyTestFolder"; DirectorySecurity dirSec = new DirectorySecurity(); dirSec.AddAccessRule( new FileSystemAccessRule(@"contoso.com\myuser", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); dirSec.SetAccessRuleProtection(false, true); Directory.CreateDirectory(dir, dirSec);
不知道它是否相关,但我用它来为用户创build主目录,因此计划是让用户拥有修改权限,并inheritance或添加具有完全权限的SYSTEM和Administrators本地用户/组。
我已经从StackOverflow尝试了很多 其他的 例子 ,但似乎没有得到那个讨厌的盒子。 有任何想法吗?
编辑:这是我所指的checkbox。 请注意,这些不是我特别的截图,只是示范。
此图像显示第二个窗口中的灰色checkbox以及第三个窗口中的可访问checkbox。 在我的情况下,当我使用上面的代码创build文件夹(并使用
SetAccessRuleProtection
或Inheritance and Propagation标志的参数的任何变体时,这两个框都未选中。
我的目的是在创build目录时检查这些GUIcheckbox(并因此将其相应的权限设置在底层),以便它看起来与图像类似。
看起来这一切都归结为操作的顺序。 看来,如果我通过提供DirectorySecurity
创建DirectorySecurity
,它将创建文件夹权限被我提供的内容完全覆盖的文件夹。 而且,由于某种原因,如果我调用SetAccessRuleProtection
来设置文件夹在创建期间提供的DirectorySecurity的可继承权限,则不需要。
但是,如果我首先创建文件夹而不提供任何权限信息,则默认情况下,该框将被创建,然后我可以根据需要添加我的AccessRule:
using System.Security.AccessControl; //... string dir = @"C:\MyTestFolder"; //If you update and add the access rules here, it doesn't work Directory.CreateDirectory(dir); //Now that the folder has been created... DirectorySecurity dirSec = Directory.GetAccessControl(dir); dirSec.AddAccessRule( new FileSystemAccessRule(@"contoso.com\myuser", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); //this isn't really necessary at this point, but wouldn't hurt: //dirSec.SetAccessRuleProtection(false, true); Directory.SetAccessControl(dir, dirSec);
我不知道为什么要按照这个特定的顺序来完成这个任务,但是如果有人有了解,我很想听听为什么。 无论如何,这使我现在工作没有问题。