我正在编写一个 C# 应用程序,需要创建一个文件夹,然后授予 DOMAIN\Users 完全权限。当我尝试传入 Environment.UserDomainName + @"\Users"
时,它会抛出 System.Security.Principal.IdentityNotMappedException
。目前,我有这个代码:
DirectoryInfo dirInfo = new DirectoryInfo(path);
DirectorySecurity dirSec = dirInfo.GetAccessControl();
// All users should have full control.
dirSec.AddAccessRule(new FileSystemAccessRule(Environment.UserDomainName + @"\Users", FileSystemRights.FullControl, AccessControlType.Allow));
dirInfo.SetAccessControl(dirSec);
如何才能使其“起作用”,例如允许用户完全控制存储在 path
中的目录?
这是完整的异常(exception):
最佳答案
我在我的电脑上做了一些测试,发现添加域用户时有效:
Environment.UserDomainName + @"\Users"
对于本地计算机帐户,我必须这样做:
@".\Users"
由于您是在家用电脑上进行测试,因此我假设您不在域中,并且正在尝试添加本地计算机用户组。
关于c# - 设置整个组对目录的访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37979501/