c# - 通过SID获取本地用户

标签 c# .net vb.net permissions directoryservices

问题:我想通过 SID 获取本地 Windows 用户:

我找到了这段代码

[ADSI]("WinNT://$Env:Computername/<SID=S-1-5-18>")

这里: http://www.eggheadcafe.com/software/aspnet/32882679/add-builtin-account-to-local-group-using-winnt-adsi-provider.aspx

我从中推断,我可以在 (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/

相关文章:

C#客户端GO服务器

c# - 带有.net的Web服务无法连接到互联网信息服务中的mysql数据库

vb.net - 如何在 VB.NET 中使用正则表达式将每个单词的第一个字符大写?

c# - Enumerable.ElementAt<TSource> 有什么意义?

vb.net - 使用 System.Reflection.Emit 在 Windows.Forms 中显示 MessageBox

asp.net-mvc - 小数和整数的 MVC 编辑器模板

c# - MVC 给出了一个奇怪的错误

c# - 使用 Windsor 3 从代码中注入(inject) AppSettings

c# - 无法将 MySQL 日期/时间值转换为 System.DateTime

c# webrequest 挂起