问题:我想通过 SID 获取本地 Windows 用户:
我找到了这段代码
[ADSI]("WinNT://$Env:Computername/<SID=S-1-5-18>")
我从中推断,我可以在 (VB) .NET 中这样做:
Dim strURL As String = "WinNT://" + strComputerName + "/<SID=" + strSID + ">"
Dim de As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry(strURL)
de.Properties("whatever").Value.ToString()
但是,这不起作用。 任何人都知道我如何对所有用户执行此 WITHOUT 循环(这需要首先从 byte[] 转换为字符串,然后比较[不区分大小写]大量字符串,这使得速度变慢)。
最佳答案
如果您使用的是 .NET 3.5 或更高版本,则可以使用以下代码(在将新的 System.DirectoryServices.AccountManagement
命名空间的引用添加到项目中之后):
using System.DirectoryServices.AccountManagement;
// create a machine-context (local machine)
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.Sid, <YourSidHere>);
if (up != null)
{
Console.WriteLine("You've found: {0}", up.DisplayName);
}
else
{
Console.WriteLine("ERROR: no one found");
}
关于c# - 通过SID获取本地用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5140468/