列出所有使用目录服务的本地用户

发布于 2024-12-16 14:47:51 字数 627 浏览 2 评论 0原文

我创建的以下方法似乎不起作用。 foreach 循环总是会发生错误。

NotSupportedException 未处理...提供程序不支持 正在搜索,无法搜索到WinNT://WIN7,computer。

我正在查询本地机器

 private static void listUser(string computer)
 {
        using (DirectoryEntry d= new DirectoryEntry("WinNT://" + 
                     Environment.MachineName + ",computer"))
        {
           DirectorySearcher ds = new DirectorySearcher(d);
            ds.Filter = ("objectClass=user");
            foreach (SearchResult s in ds.FindAll())
            {

              //display name of each user

            }
        }
    }

The following method I created seem does not work. An error always happens on foreach loop.

NotSupportedException was unhandled...The provider does not support
searching and cannot search WinNT://WIN7,computer.

I'm querying the local machine

 private static void listUser(string computer)
 {
        using (DirectoryEntry d= new DirectoryEntry("WinNT://" + 
                     Environment.MachineName + ",computer"))
        {
           DirectorySearcher ds = new DirectorySearcher(d);
            ds.Filter = ("objectClass=user");
            foreach (SearchResult s in ds.FindAll())
            {

              //display name of each user

            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2024-12-23 14:47:51

您不能将 DirectorySearcherWinNT提供商。从文档中:

使用 DirectorySearcher 对象通过轻量级目录访问协议 (LDAP) 对 Active Directory 域服务层次结构进行搜索和执行查询。 LDAP 是系统提供的唯一支持目录搜索的 Active Directory 服务接口 (ADSI) 提供程序。

相反,请使用 DirectoryEntry.Children 属性< /a> 访问 Computer 对象,然后使用 SchemaClassName 属性 查找 用户对象

使用 LINQ:

string path = string.Format("WinNT://{0},computer", Environment.MachineName);

using (DirectoryEntry computerEntry = new DirectoryEntry(path))
{
    IEnumerable<string> userNames = computerEntry.Children
        .Cast<DirectoryEntry>()
        .Where(childEntry => childEntry.SchemaClassName == "User")
        .Select(userEntry => userEntry.Name);

    foreach (string name in userNames)
        Console.WriteLine(name);
}       

不使用 LINQ:

string path = string.Format("WinNT://{0},computer", Environment.MachineName);

using (DirectoryEntry computerEntry = new DirectoryEntry(path))
    foreach (DirectoryEntry childEntry in computerEntry.Children)
        if (childEntry.SchemaClassName == "User")
            Console.WriteLine(childEntry.Name);

You cannot use a DirectorySearcher with the WinNT provider. From the documentation:

Use a DirectorySearcher object to search and perform queries against an Active Directory Domain Services hierarchy using Lightweight Directory Access Protocol (LDAP). LDAP is the only system-supplied Active Directory Service Interfaces (ADSI) provider that supports directory searching.

Instead, use the DirectoryEntry.Children property to access all child objects of your Computer object, then use the SchemaClassName property to find the children that are User objects.

With LINQ:

string path = string.Format("WinNT://{0},computer", Environment.MachineName);

using (DirectoryEntry computerEntry = new DirectoryEntry(path))
{
    IEnumerable<string> userNames = computerEntry.Children
        .Cast<DirectoryEntry>()
        .Where(childEntry => childEntry.SchemaClassName == "User")
        .Select(userEntry => userEntry.Name);

    foreach (string name in userNames)
        Console.WriteLine(name);
}       

Without LINQ:

string path = string.Format("WinNT://{0},computer", Environment.MachineName);

using (DirectoryEntry computerEntry = new DirectoryEntry(path))
    foreach (DirectoryEntry childEntry in computerEntry.Children)
        if (childEntry.SchemaClassName == "User")
            Console.WriteLine(childEntry.Name);
拥抱影子 2024-12-23 14:47:51

以下是获取本地计算机名称的几种不同方法:

string name = Environment.MachineName;
string name = System.Net.Dns.GetHostName();
string name = System.Windows.Forms.SystemInformation.ComputerName;
string name = System.Environment.GetEnvironmentVariable(“COMPUTERNAME”);

下一种是获取当前用户名的方法:

string name = System.Windows.Forms.SystemInformation.UserName;

The following are a few different ways to get your local computer name:

string name = Environment.MachineName;
string name = System.Net.Dns.GetHostName();
string name = System.Windows.Forms.SystemInformation.ComputerName;
string name = System.Environment.GetEnvironmentVariable(“COMPUTERNAME”);

The next one is a way to get the current user name:

string name = System.Windows.Forms.SystemInformation.UserName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文