从我的网上邻居获取计算机名称
本文关键字:获取 计算机名 邻居 我的 | 更新日期: 2023-09-27 18:31:37
我想知道是否有办法使用 C# 获取出现在我的网络位置中的所有计算机名称。
您需要使用 NetServerEnum() API。我不相信在基本的 .NET 库中有托管包装器,但我能够通过快速的谷歌搜索找到它:http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using
注意:我还没有测试或彻底审查代码项目代码,但如果有任何问题,它应该足以作为您需要的起点。
编辑:除非您确定域环境,否则不要使用目录服务。System.DirectoryServices 类是一个 ADSI 包装器,无需查询 Active Directory 即可工作。NetServerEnum() 适用于工作组和域,但不能保证最可靠的数据(并非所有机器都可能出现)。它依赖于计算机浏览器服务。
最好的解决方案可能是一个包装两种可能性并合并结果的类:/
这有效,但需要一段时间。
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;
}
根据用户的权限,应用程序可能会也可能不会获取这些信息。
尝试使用 ActiveDirectory。这应该为您提供有关本地网络的准确信息。
使用 System.DirectoryServices。