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)); }
public static bool IsNicMapped(ServerNicInfo nic) { if (nic.VirtualNetwork != "---Discard---" && nic.IPAddresses != null) { return((uint)nic.IPAddresses.Length > 0U); } return(false); }
public ServerNicInfo(ServerNicInfo nic) { this.IPAddresses = (string[])nic.IPAddresses.Clone(); this.IPMasks = (string[])nic.IPMasks.Clone(); this.IPGateways = nic.IPGateways != null ? (string[])nic.IPGateways.Clone() : (string[])null; this.DNSAddrs = nic.DNSAddrs != null ? (string[])nic.DNSAddrs.Clone() : (string[])null; this.BusNumber = nic.BusNumber; this.DeviceNumber = nic.DeviceNumber; this.FriendlyName = nic.FriendlyName; this.Index = nic.Index; this.NicGuid = nic.NicGuid; this.NicType = nic.NicType; this.PNPInstanceId = nic.PNPInstanceId; this.TcpIpServiceUuid = nic.TcpIpServiceUuid; this.VirtualNetwork = nic.VirtualNetwork; }