如何获取本地网络计算机列表?

发布于 2024-08-26 23:33:44 字数 259 浏览 9 评论 0原文

我正在尝试获取本地网络计算机的列表。我尝试使用 NetServerEnumWNetOpenEnum API,但这两个 API 都返回错误代码 6118 (ERROR_NO_BROWSER_SERVERS_FOUND)。不使用本地网络中的 Active Directory。

最奇怪的Windows资源管理器显示所有本地计算机没有任何问题。

还有其他方法可以获取 LAN 中的计算机列表吗?

I am trying to get a list of local network computers. I tried to use NetServerEnum and WNetOpenEnum API, but both API return error code 6118 (ERROR_NO_BROWSER_SERVERS_FOUND). Active Directory in the local network is not used.

Most odd Windows Explorer shows all local computers without any problems.

Are there other ways to get a list of computers in the LAN?

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

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

发布评论

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

评论(6

话少情深 2024-09-02 23:33:44

您将需要使用 System.DirectoryServices 命名空间并尝试以下操作:

DirectoryEntry root = new DirectoryEntry("WinNT:");

foreach (DirectoryEntry computers in root.Children)
{
    foreach (DirectoryEntry computer in computers.Children)
    {
        if (computer.Name != "Schema")
        {
             textBox1.Text += computer.Name + "\r\n";
        }
    }
}

它对我有用。

You will need to use the System.DirectoryServices namespace and try the following:

DirectoryEntry root = new DirectoryEntry("WinNT:");

foreach (DirectoryEntry computers in root.Children)
{
    foreach (DirectoryEntry computer in computers.Children)
    {
        if (computer.Name != "Schema")
        {
             textBox1.Text += computer.Name + "\r\n";
        }
    }
}

It worked for me.

自找没趣 2024-09-02 23:33:44

我找到了使用接口 IShellItem 和 CSIDL_NETWORK 的解决方案。我得到了全网络电脑。

C++:使用方法 IShellFolder::EnumObjects

C#:您可以使用 Gong Solutions Shell Library

using System.Collections;
using System.Collections.Generic;
using GongSolutions.Shell;
using GongSolutions.Shell.Interop;

    public sealed class ShellNetworkComputers : IEnumerable<string>
    {
        public IEnumerator<string> GetEnumerator()
        {
            ShellItem folder = new ShellItem((Environment.SpecialFolder)CSIDL.NETWORK);
            IEnumerator<ShellItem> e = folder.GetEnumerator(SHCONTF.FOLDERS);

            while (e.MoveNext())
            {
                Debug.Print(e.Current.ParsingName);
                yield return e.Current.ParsingName;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

I found solution using interface IShellItem with CSIDL_NETWORK. I get all network PC.

C++: use method IShellFolder::EnumObjects

C#: you can use Gong Solutions Shell Library

using System.Collections;
using System.Collections.Generic;
using GongSolutions.Shell;
using GongSolutions.Shell.Interop;

    public sealed class ShellNetworkComputers : IEnumerable<string>
    {
        public IEnumerator<string> GetEnumerator()
        {
            ShellItem folder = new ShellItem((Environment.SpecialFolder)CSIDL.NETWORK);
            IEnumerator<ShellItem> e = folder.GetEnumerator(SHCONTF.FOLDERS);

            while (e.MoveNext())
            {
                Debug.Print(e.Current.ParsingName);
                yield return e.Current.ParsingName;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
别闹i 2024-09-02 23:33:44

我用它做了一个函数。 SchemaClassName 必须是Computer

    public List<string> NetworkComputers()
    {
        return (
        from Computers 
        in (new DirectoryEntry("WinNT:")).Children
        from Computer 
        in Computers.Children
        where Computer.SchemaClassName == "Computer"
        orderby Computer.Name
        select Computer.Name).ToList;
    }

I made a function out of it. The SchemaClassName has to be Computer

    public List<string> NetworkComputers()
    {
        return (
        from Computers 
        in (new DirectoryEntry("WinNT:")).Children
        from Computer 
        in Computers.Children
        where Computer.SchemaClassName == "Computer"
        orderby Computer.Name
        select Computer.Name).ToList;
    }
停滞 2024-09-02 23:33:44

这里是使用 LINQ 查询的属性

private List<string> NetworkHosts
    {
        get
        {
            var result = new List<string>();

            var root = new DirectoryEntry("WinNT:");
            foreach (DirectoryEntry computers in root.Children)
            {
                result.AddRange(from DirectoryEntry computer in computers.Children where computer.Name != "Schema" select computer.Name);
            }
            return result;
        }
    }

Here a property that uses a LINQ query

private List<string> NetworkHosts
    {
        get
        {
            var result = new List<string>();

            var root = new DirectoryEntry("WinNT:");
            foreach (DirectoryEntry computers in root.Children)
            {
                result.AddRange(from DirectoryEntry computer in computers.Children where computer.Name != "Schema" select computer.Name);
            }
            return result;
        }
    }
给不了的爱 2024-09-02 23:33:44

如果您不太喜欢 LINQ 查询样式语法并且还希望将工作组作为选项包含在内,那么对 toddmo 的答案进行了一个小扩展:

public IEnumerable<string> VisibleComputers(bool workgroupOnly = false) {
        Func<string, IEnumerable<DirectoryEntry>> immediateChildren = key => new DirectoryEntry("WinNT:" + key)
                .Children
                .Cast<DirectoryEntry>();
        Func<IEnumerable<DirectoryEntry>, IEnumerable<string>> qualifyAndSelect = entries => entries.Where(c => c.SchemaClassName == "Computer")
                .Select(c => c.Name);
        return (
            !workgroupOnly ?
                qualifyAndSelect(immediateChildren(String.Empty)
                    .SelectMany(d => d.Children.Cast<DirectoryEntry>())) 
                :
                qualifyAndSelect(immediateChildren("//WORKGROUP"))
        ).ToArray();
    }

A minor extension to toddmo's answer, if you don't really like LINQ query style syntax and want to also include workgroups as an option:

public IEnumerable<string> VisibleComputers(bool workgroupOnly = false) {
        Func<string, IEnumerable<DirectoryEntry>> immediateChildren = key => new DirectoryEntry("WinNT:" + key)
                .Children
                .Cast<DirectoryEntry>();
        Func<IEnumerable<DirectoryEntry>, IEnumerable<string>> qualifyAndSelect = entries => entries.Where(c => c.SchemaClassName == "Computer")
                .Select(c => c.Name);
        return (
            !workgroupOnly ?
                qualifyAndSelect(immediateChildren(String.Empty)
                    .SelectMany(d => d.Children.Cast<DirectoryEntry>())) 
                :
                qualifyAndSelect(immediateChildren("//WORKGROUP"))
        ).ToArray();
    }
忆依然 2024-09-02 23:33:44

使用 LINQ lambda 语法和非托管资源解析器的解决方案

 public List<String> GetNetworkHostNames()
 {
        using (var directoryEntry = new DirectoryEntry("WinNT:"))
        {
            return directoryEntry
                     .Children
                     .Cast<DirectoryEntry>()
                     .SelectMany(x=>x.Children.Cast<DirectoryEntry>())
                     .Where(c => c.SchemaClassName == "Computer")
                     .Select(c => c.Name)
                     .ToList();
        }
 }

Solution with LINQ lambda syntax and unmanaged resource resolver

 public List<String> GetNetworkHostNames()
 {
        using (var directoryEntry = new DirectoryEntry("WinNT:"))
        {
            return directoryEntry
                     .Children
                     .Cast<DirectoryEntry>()
                     .SelectMany(x=>x.Children.Cast<DirectoryEntry>())
                     .Where(c => c.SchemaClassName == "Computer")
                     .Select(c => c.Name)
                     .ToList();
        }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文