public static List <NetworkAdapter> GetAllNetworkAdapter() { List <NetworkAdapter> allNetworkAdapter = new List <NetworkAdapter>(); string strWQuery = "SELECT DeviceID, ProductName, " + "NetEnabled, NetConnectionStatus " + "FROM Win32_NetworkAdapter "; 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) { } } return(allNetworkAdapter); }
public static List <Keyboard> GetAllKeyboards() { List <Keyboard> allKeyboards = new List <Keyboard>(); string strWQuery = "SELECT DeviceID, Description, " + "Status " + "FROM Win32_Keyboard "; ManagementObjectCollection keyboards = WMIOperation.WMIQuery(strWQuery); foreach (ManagementObject moKeyboard in keyboards) { try { allKeyboards.Add(new Keyboard( moKeyboard["DeviceID"].ToString(), moKeyboard["Description"].ToString(), moKeyboard["Status"].ToString() )); } catch (NullReferenceException) { } } return(allKeyboards); }
public Keyboard(string deviceID) { ManagementObject crtKeyboard = new ManagementObject(); string strWQuery = string.Format("SELECT DeviceID, Description, " + " Status " + "FROM Win32_Keyboard " + "WHERE DeviceID = {0}", deviceID); try { ManagementObjectCollection keyboards = WMIOperation.WMIQuery(strWQuery); foreach (ManagementObject keyboard in keyboards) { crtKeyboard = keyboard; break; } DeviceID = deviceID; Name = crtKeyboard["Description"].ToString(); Status = crtKeyboard["Status"].ToString(); } catch (NullReferenceException) { DeviceID = string.Empty; Name = string.Empty; Status = string.Empty; } }
public int EnableOrDisableNetworkAdapter(string strOperation) { int resultEnableDisableNetworkAdapter = (int)EnumEnableDisableResult.Unknow; 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) { crtNetworkAdapter = networkAdapter; } crtNetworkAdapter.InvokeMethod(strOperation, null); Thread.Sleep(500); while (GetNetEnabled() != ((strOperation.Trim() == "Enable") ? (int)EnumNetEnabledStatus.Enabled : (int)EnumNetEnabledStatus.Disabled)) { Thread.Sleep(100); } resultEnableDisableNetworkAdapter = (int)EnumEnableDisableResult.Success; } catch (NullReferenceException) { resultEnableDisableNetworkAdapter = (int)EnumEnableDisableResult.Fail; } crtNetworkAdapter.Dispose(); return(resultEnableDisableNetworkAdapter); }
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) { 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) { DeviceId = -1; Name = string.Empty; NetEnabled = 0; NetConnectionStatus = -1; } }
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); }