开发者

Can't set FullControl on directory

开发者 https://www.devze.com 2023-02-21 01:05 出处:网络
I\'m trying to set FullControl for Everyone programmatically on a Windows 7 box with no luck. var dirSec = dir.GetAccessControl();

I'm trying to set FullControl for Everyone programmatically on a Windows 7 box with no luck.

var dirSec = dir.GetAccessControl();
var fsar = new FileSystemAccessRule(
    "Everyone",
    FileSystemRights.FullControl,
    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
    Propa开发者_如何转开发gationFlags.InheritOnly, 
    AccessControlType.Allow);

dirSec.AddAccessRule(fsar);
dir.SetAccessControl(dirSec);

This adds some permissions for the Everyone group (List and Read), but not full control. If I edit the security permissions using Explorer I can set it to FullControl. Any ideas why it's failing? There are no error messages from my attempts.


This should work fine:

    string path = @"C:\test";
    DirectorySecurity ds = Directory.GetAccessControl(path);
    ds.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
    Directory.SetAccessControl(path, ds);

If that does not work then it seems that your application does not have rights to give that kind access rule. Try to implement impersonation. Here is a sample: WindowsIdentity Impersonation using C# Code


foreach (FileSystemRights permission in Enum.GetValues(typeof(FileSystemRights)))
{
    myDirectorySecurity.AddAccessRule(
        new FileSystemAccessRule(user,
                                 permission,
                                 InheritanceFlags.ContainerInherit |
                                     InheritanceFlags.ObjectInherit |
                                     InheritanceFlags.None,
                                 PropagationFlags.None,
                                 AccessControlType.Allow));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消