none
C# 获取局域网计算机(跟微软资源管理器网络下显示一样)问题 RRS feed

  • 问题

  • 首先我尝试了以下几种方式:

    1.netapi32 的NetServerEnum() API获取,有时候会获取不到,但本地网络中是有的。还有种情况是本地网络发现时关闭的情况下也能获取到局域网计算机。具体方法http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using

    2.尝试了System.DirectoryServices class获取计算机。

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

    这个方法我本地根本获取不到计算机,我本地网络也是显示有计算机的

    3.另外参考了http://www.pinvoke.net/default.aspx/netapi32.netserverenum

    http://stackoverflow.com/questions/2557551/how-get-list-of-local-network-computers

    http://stackoverflow.com/questions/10236249/getting-computer-names-from-my-network-places

    EDIT: Do not use DirectoryServices unless your sure of a domain environment. The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). It relies on the Computer Browser service.

    The best solution would probably be a class that wraps both possibilities and merges the results :/

    这里也提到有这样的问题。但不知道如何去解决,

    重申下我的问题:我希望C#获取的网络计算机跟我本地计算机资源管理器中网络中计算机显示结果一样

    2012年11月15日 2:39

全部回复