【发布时间】:2011-07-31 16:03:42
【问题描述】:
关于获取本地机器的名称和 IP 地址有很多问题,还有一些关于获取 LAN 上其他机器的 IP 地址的问题(并非所有问题都正确回答)。这是不同的。
在 Windows 资源管理器中,如果我选择侧栏上的网络,我会看到按机器名称列出的 LAN 上的本地机器(无论如何,在 Windows 工作组中)。如何在 C# 中以编程方式获取相同的信息?
【问题讨论】:
关于获取本地机器的名称和 IP 地址有很多问题,还有一些关于获取 LAN 上其他机器的 IP 地址的问题(并非所有问题都正确回答)。这是不同的。
在 Windows 资源管理器中,如果我选择侧栏上的网络,我会看到按机器名称列出的 LAN 上的本地机器(无论如何,在 Windows 工作组中)。如何在 C# 中以编程方式获取相同的信息?
【问题讨论】:
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;
}
【讨论】:
您可以尝试使用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);
}
}
}
【讨论】:
您需要为给定范围内的所有 IP 广播 ARP 请求。首先在您的网络上定义基本 IP,然后设置一个上位标识符。
我本来打算写一些代码示例等,但看起来有人在这里全面介绍了这一点;
【讨论】:
这似乎是你所追求的:How get list of local network computers?
在 C# 中:您可以使用 Gong 解决方案 外壳库 (https://sourceforge.net/projects/gong-shell/)
【讨论】: