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

Kin*_*gon 18 c# c++ networking winapi

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

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

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

小智 14

您将需要使用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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它对我有用.

  • 嗯,它还列出了每个域用户和域组 (5认同)
  • 是的,需要在`if`语句中添加`computer.SchemaClassName =="Computer".否则,效果很好! (3认同)

Kin*_*gon 11

我使用接口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();
        }
    }
Run Code Online (Sandbox Code Playgroud)


tod*_*dmo 6

我用它做了一个功能.本SchemaClassName必须是计算机

    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;
    }
Run Code Online (Sandbox Code Playgroud)