1

我在 C# 中的小程序中为 Windows上的目录文件夹“ wwwroot ”上的“ Everyone ”分配访问权限时遇到问题。这是我的做法。

//I also try with 'S-1-1-0'/'Everyone' but it's the same result 
string userPermission = "Everyone"  ;

DirectoryInfo myDirRoot = new DirectoryInfo(myArmsUpdate.InstallationPath);
DirectorySecurity myDirectorySecurity = myDirRoot.GetAccessControl();
FileSystemAccessRule myPermission = new FileSystemAccessRule(userPermission , FileSystemRights.ReadAndExecute, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);

myDirectorySecurity.AddAccessRule(myPermission);
myDirRoot.SetAccessControl(myDirectorySecurity);

但是我仍然遇到同样的错误:

System.Security.Principal.IdentityNotMappedException

4

1 回答 1

2

试试下面的,

DirectorySecurity sec = Directory.GetAccessControl(path);
        // Using this instead of the "Everyone" string means we work on non-English systems.
        SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
        sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        Directory.SetAccessControl(path, sec);

注意:您必须是管理员才能执行此操作

于 2013-02-08T09:52:50.090 回答