我遇到一个问题,我需要为所有经过身份验证的用户添加对文件夹的访问权限,以存储与应用程序相关的设置。我发现这可以用下面的代码来完成...
var Info = new DirectoryInfo(settingsdir);
var Security = Info.GetAccessControl(AccessControlSections.Access);
Security.AddAccessRule(
new FileSystemAccessRule(
"Authenticated Users", FileSystemRights.Modify,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
我发现的问题是,“Authenticated Users”是 Windows 上的一个系统帐户,但是,在不同语言版本的 Windows 上,此帐户名称被翻译,例如在德国,此帐户称为“Authentifizierte Benutzer”。有没有办法知道这个帐户的正确名称(无需遍历每种语言并找到正确的帐户名称)。
最佳答案
我建议您使用 Well Known SID 列表(参见 http://support.microsoft.com/kb/243330)。经过身份验证的用户始终是 SID:S-1-5-11。如果您使用它,它应该与语言无关(但我没有测试过)。
创建一个 SecurityIdentifier并改用它:
var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
Security.AddAccessRule(
new FileSystemAccessRule(
sid,
FileSystemRights.Modify,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow));
关于c# - 以编程方式向文件夹添加权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478917/