在我的 C# 代码中,我想将一个用户添加到“管理员”组。我听说在德语版的 Windows 上,该组被称为“Administratoren”,也许在其他本地版本上它会有其他名称。
我的代码在进行搜索时传递了一个硬编码的字符串:
var context = new PrincipalContext( ContextType.Machine );
var group = GroupPrincipal.FindByIdentity( context, "Administrators" );
因此,如果该组实际上有其他名称,它就会中断。我找到了 this MSDN article with well-known SIDs nut 我不知道如何实际使用它们来解决我的问题。
如何找到独立于 Windows 操作系统语言的本地组?
最佳答案
我不知道这是否有帮助。
using System.Security;
using System.Security.Principal;
......
SecurityIdentifier sid = new SecurityIdentifier("S-1-5-32-544");
string name = sid.Translate(typeof(NTAccount)).Value;
Console.WriteLine(name);
结果是
"BUILTIN\Administrators"
我从 this page 中获取了 SID您可以在其中找到其他值进行试验。
关于c# - 如何以独立于区域设置的方式定位本地组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11825089/