public NetworkAdapter(int deviceId) { ManagementObject crtNetworkAdapter = new ManagementObject(); string strWQuery = string.Format("SELECT DeviceID, ProductName, " + "NetEnabled, NetConnectionStatus " + "FROM Win32_NetworkAdapter " + "WHERE DeviceID = {0}", deviceId); try { ManagementObjectCollection networkAdapters = WMIOperation.WMIQuery(strWQuery); foreach (ManagementObject networkAdapter in networkAdapters) { // Expected to be only one ManagementObject instance. crtNetworkAdapter = networkAdapter; break; } DeviceId = deviceId; Name = crtNetworkAdapter["ProductName"].ToString(); NetEnabled = ( Convert.ToBoolean(crtNetworkAdapter["NetEnabled"].ToString())) ? (int)EnumNetEnabledStatus.Enabled : (int)EnumNetEnabledStatus.Disabled; NetConnectionStatus = Convert.ToInt32( crtNetworkAdapter["NetConnectionStatus"].ToString()); } catch (NullReferenceException) { // If there is no a network adapter which deviceid equates to the argument // "deviceId" just to construct a none exists network adapter DeviceId = -1; Name = string.Empty; NetEnabled = 0; NetConnectionStatus = -1; } }
/// <summary> /// List all the NetworkAdapters /// </summary> /// <returns>The list of all NetworkAdapter of the machine</returns> public static List <NetworkAdapter> GetAllNetworkAdapter() { List <NetworkAdapter> allNetworkAdapter = new List <NetworkAdapter>(); // Manufacturer <> 'Microsoft'to get all none virtual devices. // Because the AdapterType property will be null if the NetworkAdapter is // disabled, so we do not use NetworkAdapter = 'Ethernet 802.3' or // NetworkAdapter = 'Wireless’ string strWQuery = "SELECT DeviceID, ProductName, " + "NetEnabled, NetConnectionStatus " + "FROM Win32_NetworkAdapter " + "WHERE Manufacturer <> 'Microsoft'"; ManagementObjectCollection networkAdapters = WMIOperation.WMIQuery(strWQuery); foreach (ManagementObject moNetworkAdapter in networkAdapters) { try { allNetworkAdapter.Add(new NetworkAdapter( Convert.ToInt32(moNetworkAdapter["DeviceID"].ToString()), moNetworkAdapter["ProductName"].ToString(), (Convert.ToBoolean(moNetworkAdapter["NetEnabled"].ToString())) ? (int)EnumNetEnabledStatus.Enabled : (int)EnumNetEnabledStatus.Disabled, Convert.ToInt32(moNetworkAdapter["NetConnectionStatus"].ToString() ))); } catch (NullReferenceException) { // Ignore some other devices (like the bluetooth), that need user // interaction to enable or disable. } } return(allNetworkAdapter); }
/// <summary> /// Get the NetworkAdapter Netenabled Property /// </summary> /// <returns>Whether the NetworkAdapter is enabled</returns> public int GetNetEnabled() { int netEnabled = (int)EnumNetEnabledStatus.Unknow; string strWQuery = string.Format("SELECT NetEnabled FROM Win32_NetworkAdapter " + "WHERE DeviceID = {0}", DeviceId); try { ManagementObjectCollection networkAdapters = WMIOperation.WMIQuery(strWQuery); foreach (ManagementObject networkAdapter in networkAdapters) { netEnabled = (Convert.ToBoolean(networkAdapter["NetEnabled"].ToString())) ? (int)EnumNetEnabledStatus.Enabled : (int)EnumNetEnabledStatus.Disabled; } } catch (NullReferenceException) { // If NullReferenceException return (EnumNetEnabledStatus.Unknow) } return(netEnabled); }