问:我想通过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()然而,这是行不通的。任何人都知道我如何在不需要对所有用户进行循环的情况下执行这个(这需要首先将byte[]转换为string,然后比较大小写不敏感的很多字符串,这使得它变得很慢)。
发布于 2011-02-28 10:51:43
如果您使用的是.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");
}发布于 2011-02-28 10:37:31
Win32有LookupAccountSid函数,它正是这样做的。
有关如何从PInvoke中使用域名,请参见VB.NET。在您的示例中,域名将是本地计算机名。
https://stackoverflow.com/questions/5140468
复制相似问题