public static ServerNicInfo[] GetNetworkAdapterList(ManagementScope scope)
    {
        Dictionary <string, ServerNicInfo> dictionary = new Dictionary <string, ServerNicInfo>((IEqualityComparer <string>)StringComparer.CurrentCultureIgnoreCase);
        ObjectQuery query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = TRUE");

        using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(scope, query))
        {
            using (managementObjectSearcher.Get())
            {
                foreach (ManagementObject managementObject in managementObjectSearcher.Get())
                {
                    ServerNicInfo serverNicInfo = new ServerNicInfo();
                    serverNicInfo.IPAddresses      = (string[])managementObject["IPAddress"];
                    serverNicInfo.IPMasks          = (string[])managementObject["IPSubnet"];
                    serverNicInfo.IPGateways       = managementObject["DefaultIPGateway"] != null ? (string[])managementObject["DefaultIPGateway"] : (string[])null;
                    serverNicInfo.DNSAddrs         = managementObject["DNSServerSearchOrder"] != null ? (string[])managementObject["DNSServerSearchOrder"] : (string[])null;
                    serverNicInfo.DNSDomain        = (string)managementObject.Properties["DNSDomain"].Value;
                    serverNicInfo.TcpIpServiceUuid = (string)managementObject["SettingID"];
                    serverNicInfo.Index            = (int)(uint)managementObject["Index"];
                    serverNicInfo.PNPInstanceId    = WMIUtils.GetPNPInstanceId(scope, serverNicInfo.Index);
                    serverNicInfo.DHCPEnabled      = (bool)managementObject["DHCPEnabled"];
                    ManagementObject firstElement = WMIUtils.GetFirstElement(managementObject.GetRelated("Win32_NetworkAdapter"));
                    serverNicInfo.FriendlyName = (string)firstElement["NetConnectionID"];
                    if (!string.IsNullOrEmpty(serverNicInfo.PNPInstanceId) && !dictionary.ContainsKey(serverNicInfo.TcpIpServiceUuid))
                    {
                        dictionary.Add(serverNicInfo.TcpIpServiceUuid, serverNicInfo);
                    }
                }
            }
        }
        return(CUtils.CollectionToArray <ServerNicInfo>((ICollection <ServerNicInfo>)dictionary.Values));
    }
Exemple #2
0
 public ManagementObject GetRelatedFirst(string relatedClass)
 {
     using (ManagementObjectCollection related = this._mo.GetRelated(relatedClass))
         return(WMIUtils.GetFirstElement(related));
 }
Exemple #3
0
 public static ManagementObject QueryFirst(ManagementScope scope, string query)
 {
     return(WMIUtils.GetFirstElement(WMIUtils.Query(scope, query)));
 }