从我的网络位置获取计算机名称

我想知道是否有办法使用C#获取显示在我的网络位置的所有计算机名称。

您将需要使用NetServerEnum()API。 我不相信在基础.NET库中有一个托管包装,但我能够通过快速谷歌搜索找到它: http : //www.codeproject.com/Articles/16113/Retreiving-a-list-of -网络,计算机名,使用

注意 :我没有测试或彻底检查过代码项目代码,但如果出现任何问题,它应该足以作为您需要的起点。

编辑: 除非您确定域环境,否则不要使用DirectoryServices 。 System.DirectoryServices类是一个ADSI包装器,无需Active Directory进行查询即可运行。 NetServerEnum()适用于工作组和域,但不保证最可靠的数据(并非所有计算机都可以显示)。 它依赖于计算机浏览器服务。

最好的解决方案可能是包含两种可能性并合并结果的类:/

这有效,但需要一段时间。 :/

public List ListNetworkComputers() { List _ComputerNames = new List(); 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。