简体   繁体   English

从网上邻居获取计算机名称

[英]Getting computer names from my network places

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

This works, but it takes a while. 可以,但是需要一段时间。 :/ :/

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

You will want to use the NetServerEnum() API. 您将要使用NetServerEnum()API。 I dont believe there is a managed wrapper for this in the base .NET libraries but I was able to find this with a quick google search: http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using 我不相信在基本.NET库中对此有托管包装,但是我可以通过快速的Google搜索找到它: http : //www.codeproject.com/Articles/16113/Retreiving-a-list-of -网络计算机名称使用

NOTE : I haven't tested or thoroughly reviewed the codeproject code but it should be enough of a starting point for what you need if there are any issues. 注意 :我尚未测试或彻底检查过代码项目代码,但是如果有任何问题,它应该足以满足您的需求。

EDIT: Do not use DirectoryServices unless your sure of a domain environment . 编辑: 除非您确定域环境,否则不要使用DirectoryServices The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. System.DirectoryServices类是ADSI包装程序,无需工作的Active Directory即可工作。 NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). NetServerEnum()可在工作组和域上工作,但不能保证最可靠的数据(并非所有机器都可以显示)。 It relies on the Computer Browser service. 它依赖于计算机浏览器服务。

The best solution would probably be a class that wraps both possibilities and merges the results :/ 最好的解决方案可能是包装所有可能性并合并结果的类:/

Depends on the user's permission, the application may or may not get those information. 取决于用户的许可,应用程序可能会或可能不会获得这些信息。

Try using ActiveDirectory. 尝试使用ActiveDirectory。 This should get you precise information about the local network. 这应该为您提供有关本地网络的准确信息。

Use System.DirectoryServices. 使用System.DirectoryServices。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM