/// <summary> /// Gets the list from WMI. /// </summary> /// <returns></returns> protected static List <WindowsNetworkCard> GetListFromWMI() { SortedDictionary <uint, WindowsNetworkCard> dictionary = new SortedDictionary <uint, WindowsNetworkCard>(); List <WindowsNetworkCard> lista = new List <WindowsNetworkCard>(); SelectQuery query = new SelectQuery("Win32_NetworkAdapter");//, "GUID is not null"); ManagementObjectSearcher search = new ManagementObjectSearcher(query); // 1 - Create list of cards WindowsNetworkCard card; // create network card objects foreach (ManagementObject item in search.Get()) { WmiNetworkAdapter adapter = new WmiNetworkAdapter(item); /*if (String.IsNullOrEmpty(adapter.GUID)) * { * continue; * }*/ if (adapter.Name.ToUpper().Contains("Microsoft Virtual WiFi Miniport Adapter".ToUpper())) { continue; } ; card = new WindowsNetworkCard(); //GUID is not supported in winxp //card.Id = adapter.GUID; card.ViewId = fixViewId(adapter.Caption); card.Name = adapter.Name; card.HardwareName = fixHardwareName(adapter.Caption); //card.Enabled = adapter.NetEnabled; card.NetConnectionStatus = adapter.NetConnectionStatus; card.Index = adapter.Index; card.MaxSpeed = adapter.MaxSpeed; card.PnpDeviceId = adapter.PNPDeviceID; //http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx /* * "Ethernet 802.3" * "Token Ring 802.5" * "Fiber Distributed Data Interface (FDDI)" * "Wide Area Network (WAN)" * "LocalTalk" * "Ethernet using DIX header format" * "ARCNET" * "ARCNET (878.2)" * "ATM" * "Wireless" * "Infrared Wireless" * "Bpc" * "CoWan" * "1394" * */ card.AdapterType = adapter.AdapterType; card.Description = adapter.NetConnectionID; card.MacAddress = adapter.MACAddress; dictionary[card.Index] = card; } // 2 - Get more info String id; SelectQuery query2 = new SelectQuery("Win32_NetworkAdapterConfiguration"); ManagementObjectSearcher search2 = new ManagementObjectSearcher(query2); // find by index foreach (ManagementObject item in search2.Get()) { WmiNetworkAdapterConfiguration adapterConfigurator = new WmiNetworkAdapterConfiguration(item); id = adapterConfigurator.SettingID; Debug.WriteLine("Config for " + adapterConfigurator.SettingID); card = dictionary[adapterConfigurator.Index]; if (card != null) { // set the uid card.Id = adapterConfigurator.SettingID; if (!IsNetworkCardInRegistry(card)) { // it's not a network card dictionary.Remove(adapterConfigurator.Index); Debug.WriteLine("Config for " + adapterConfigurator.Caption + " is not a network card"); continue; } card.WinsEnableLMHostsLookup = adapterConfigurator.WINSEnableLMHostsLookup; card.WinsHostLookupFile = adapterConfigurator.WINSHostLookupFile; card.WinsPrimaryServer = adapterConfigurator.WINSPrimaryServer; card.WinsSecondaryServer = adapterConfigurator.WINSSecondaryServer; } else { Debug.WriteLine("Config for " + adapterConfigurator.Caption + " not found"); } } // every item withoud id is not a valid adapter foreach (WindowsNetworkCard item in dictionary.Values) { if (!String.IsNullOrEmpty(item.Id)) { MapDataFromRegistry(item); lista.Add(item); } } return(lista); }
/// <summary> /// Gets the list from WMI. /// </summary> /// <returns></returns> protected static List <WindowsNetworkCard> GetListFromWMI() { SortedDictionary <string, WindowsNetworkCard> dictionary = new SortedDictionary <string, WindowsNetworkCard>(); List <WindowsNetworkCard> lista = new List <WindowsNetworkCard>(); SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "GUID is not null"); ManagementObjectSearcher search = new ManagementObjectSearcher(query); // 1 - Create list of cards WindowsNetworkCard card; // create network card objects foreach (ManagementObject item in search.Get()) { WmiNetworkAdapter adapter = new WmiNetworkAdapter(item); if (adapter.Name.ToUpper().Contains("Microsoft Virtual WiFi Miniport Adapter".ToUpper())) { continue; } ; card = new WindowsNetworkCard(); //GUID is supported in win7 card.Id = adapter.GUID; card.ViewId = fixViewId(adapter.Caption); card.Name = adapter.Name; card.HardwareName = fixHardwareName(adapter.Caption); card.Enabled = adapter.NetEnabled; card.NetConnectionStatus = adapter.NetConnectionStatus; card.Index = adapter.Index; card.MaxSpeed = adapter.Speed; card.PnpDeviceId = adapter.PNPDeviceID; card.Description = adapter.NetConnectionID; card.MacAddress = adapter.MACAddress; // http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216(v=vs.85).aspx /* * "Ethernet 802.3" * "Token Ring 802.5" * "Fiber Distributed Data Interface (FDDI)" * "Wide Area Network (WAN)" * "LocalTalk" * "Ethernet using DIX header format" * "ARCNET" * "ARCNET (878.2)" * "ATM" * "Wireless" * "Infrared Wireless" * "Bpc" * "CoWan" * "1394" * */ card.AdapterType = adapter.AdapterType; if (!IsNetworkCardInRegistry(card)) { continue; } // 2 - get more info from registry MapDataFromRegistry(card); dictionary[card.Id] = card; } // 2 - Get more info String id; SelectQuery query2 = new SelectQuery("Win32_NetworkAdapterConfiguration", "IPEnabled='TRUE'"); ManagementObjectSearcher search2 = new ManagementObjectSearcher(query2); // find by index foreach (ManagementObject item in search2.Get()) { WmiNetworkAdapterConfiguration adapterConfigurator = new WmiNetworkAdapterConfiguration(item); id = adapterConfigurator.SettingID; // only if card is present if (dictionary.ContainsKey(id)) { card = dictionary[id]; if (card != null) { card.WinsEnableLMHostsLookup = adapterConfigurator.WINSEnableLMHostsLookup; card.WinsHostLookupFile = adapterConfigurator.WINSHostLookupFile; card.WinsPrimaryServer = adapterConfigurator.WINSPrimaryServer; card.WinsSecondaryServer = adapterConfigurator.WINSSecondaryServer; } } } lista.AddRange(dictionary.Values); return(lista); }