在 C# 中,如何获取本地计算机名称列表,例如在 Windows 资源管理器中查看网络的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5581140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:34:57  来源:igfitidea点击:

In C# how do I get the list of local computer names like what one gets viewing the Network in windows explorer

c#windowslanworkgroup

提问by Alex McBride

There are a lot of questions about getting the name and IP addresses of the local machine and several about getting IP addresses of other machines on the LAN (not all answered correctly). This is different.

有很多关于获取本地机器的名称和IP地址的问题以及一些关于获取LAN上其他机器的IP地址的问题(并非所有答案都正确)。这是不同的。

In windows explorer if I select Network on the side bar I get a view of local machines on my LAN listed by machine name (in a windows workgroup, anyway). How do I get that same information programatically in C#?

在 Windows 资源管理器中,如果我在侧栏上选择网络,我会看到 LAN 上按机器名称列出的本地机器(无论如何在 Windows 工作组中)。如何在 C# 中以编程方式获取相同的信息?

回答by Alex McBride

You can try using the System.DirectoryServicesnamespace.

您可以尝试使用System.DirectoryServices命名空间。

var root = new DirectoryEntry("WinNT:");
foreach (var dom in root.Children) {
    foreach (var entry in dom.Children) {
        if (entry.Name != "Schema") {
            Console.WriteLine(entry.Name);
        }
    }
}

回答by Brian Scott

You need to broadcast an ARP request for all IPs within a given range. Start by defining the base IP on your network and then setting an upper identifier.

您需要为给定范围内的所有 IP 广播 ARP 请求。首先定义网络上的基本 IP,然后设置上层标识符。

I was going to write up some code examples etc but it looks like someone has covered this comprehensively here;

我打算写一些代码示例等,但看起来有人在这里全面介绍了这一点;

Stackoverflow ARP question

Stackoverflow ARP 问题

回答by tomahawk

This seems to be what you are after: How get list of local network computers?

这似乎是您所追求的:如何获取本地网络计算机的列表?

In C#: you can use Gong Solutions Shell Library (https://sourceforge.net/projects/gong-shell/)

在 C# 中:您可以使用Gong Solutions Shell Library ( https://sourceforge.net/projects/gong-shell/)

回答by tohid badri

public List<String> ListNetworkComputers()
{
    List<String> _ComputerNames = new List<String>();
    String _ComputerSchema = "Computer";
    System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:");
    foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children)
    {
        foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children)
        {
            if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower()))
            {
                _ComputerNames.Add(_PCNameEntry.Name);
            }
        }
    }
    return _ComputerNames;
}