2012-04-19 136 views
3

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

+1

Duplicate question:http://stackoverflow.com/questions/5581140/in-c-sharp-how-do-i-get-the-list-of-local-computer-names-like-what-one- gets-view – 2012-04-19 20:34:35

+0

甚至是那个显然是从http://stackoverflow.com/questions/2557551/how-get-list-of-local-network-computers/2562302#2562302被骗的人。 – 2012-04-19 20:35:55

回答

3

您将需要使用NetServerEnum()API。我不相信这是在基本.NET库这个托管的包装,但我可以用快速谷歌搜索发现这一点:http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using

:我没有测试或彻底审查CodeProject上的代码,但如果有任何问题,它应该足以作为你所需要的起点。

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

最好的解决方案很可能是一个包装两种可能性和合并结果的类:/

+0

这可能是一个非常愚蠢的问题,但是,仅仅执行'net view> computerListing.txt'使用StreamReader读取它并完成它会不会更容易? – 2014-01-31 09:06:19

+0

非常感谢!你的解释真的帮助我理解! – 2018-01-01 10:29:12

3

这可行,但需要一段时间。 :/

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; 
} 
2

取决于用户的权限,应用程序可能会或可能不会得到这些信息。

尝试使用ActiveDirectory。这应该会为您提供关于本地网络的准确信息。

使用System.DirectoryServices。