无法为文件夹设置完全控制权限

我正在尝试Full Control通过 C# 将权限(对于 NT 服务帐户)添加到文件夹。但是,没有设置权限,我在这里缺少什么?


var directoryInfo = new DirectoryInfo(@"C:\Test");

var directorySecurity = directoryInfo.GetAccessControl();


directorySecurity.AddAccessRule(new FileSystemAccessRule("NT Service\\FileMoverService",

    FileSystemRights.FullControl, AccessControlType.Allow));


directoryInfo.SetAccessControl(directorySecurity);

http://img.mukewang.com/6370916a000184f303500377.jpg

海绵宝宝撒
浏览 197回答 2
2回答

PIPIONE

您需要指定继承标志:directorySecurity.AddAccessRule(new FileSystemAccessRule(@"NT Service\FileMoverService",    FileSystemRights.FullControl,    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,    PropagationFlags.None,    AccessControlType.Allow));

DIEA

该方法GrantFullControl可用于设置Full Control给定目录和用户的权限。private static void GrantFullControl(string directoryPath, string username){    if (!Directory.Exists(directoryPath))        return;    var directorySecurity = Directory.GetAccessControl(directoryPath);    directorySecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl,        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None,        AccessControlType.Allow));    Directory.SetAccessControl(directoryPath, directorySecurity);}只需调用如下所示的方法。GrantFullControl(@"C:\Test", @"NT Service\FileMoverService");
打开App,查看更多内容
随时随地看视频慕课网APP