GooliveR Asked:2020-10-10 06:39:44 +0000 UTC2020-10-10 06:39:44 +0000 UTC 2020-10-10 06:39:44 +0000 UTC 以编程方式禁用网络适配器 772 你好! 告诉我如何在 [c#] 中以编程方式禁用设备 从网络? c# 1 个回答 Voted Best Answer Akubik 2020-10-10T07:54:07Z2020-10-10T07:54:07Z 使用 WMI 启用/禁用网络适配器 (CSWMIEnableDisableNetworkAdapter) (针对 VS2010 设计和测试的示例) 1. NetworkAdapter 类构造函数 internal class NetworkAdapter { #region NetworkAdapter Properties /// <summary> /// The DeviceId of the NetworkAdapter /// </summary> public int DeviceId { get; private set; } /// <summary> /// The ProductName of the NetworkAdapter /// </summary> public string Name { get; private set; } /// <summary> /// The NetEnabled status of the NetworkAdapter /// </summary> public int NetEnabled { get; private set; } /// <summary> /// The Net Connection Status Value /// </summary> public int NetConnectionStatus { get; private set; } /// <summary> /// The Net Connection Status Description /// </summary> public static string[] SaNetConnectionStatus = { Resources.NetConnectionStatus0, Resources.NetConnectionStatus1, Resources.NetConnectionStatus2, Resources.NetConnectionStatus3, Resources.NetConnectionStatus4, Resources.NetConnectionStatus5, Resources.NetConnectionStatus6, Resources.NetConnectionStatus7, Resources.NetConnectionStatus8, Resources.NetConnectionStatus9, Resources.NetConnectionStatus10, Resources.NetConnectionStatus11, Resources.NetConnectionStatus12 }; /// <summary> /// Enum the NetEnabled Status /// </summary> private enum EnumNetEnabledStatus { Disabled = -1, Unknow, Enabled } /// <summary> /// Enum the Operation reuslt of Enable and Disable Network Adapter /// </summary> private enum EnumEnableDisableResult { Fail = -1, Unknow, Success } #endregion #region Construct NetworkAdapter public NetworkAdapter(int deviceId, string name, int netEnabled, int netConnectionStatus) { DeviceId = deviceId; Name = name; NetEnabled = netEnabled; NetConnectionStatus = netConnectionStatus; } } 2. 此代码查找所有适配器。 List<NetworkAdapter> allNetworkAdapter = new List<NetworkAdapter>(); string strWQuery = "SELECT DeviceID, ProductName, Description, " + "NetEnabled, NetConnectionStatus " + "FROM Win32_NetworkAdapter " + "WHERE Manufacturer <> 'Microsoft' "; ObjectQuery oQuery = new System.Management.ObjectQuery(strWQuery); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); foreach (ManagementObject mo in oReturnCollection) { netEnabled = (Convert.ToBoolean(mo["NetEnabled"].ToString())) ? 1 : -1; allNetworkAdapter.Add(new NetworkAdapter( Convert.ToInt32(mo["DeviceID"].ToString()), mo["ProductName"].ToString(), mo["Description"].ToString(), netEnabled, Convert.ToInt32(mo["NetConnectionStatus"].ToString()))); }; return allNetworkAdapter; 3. 此代码打开和关闭网络适配器。 ManagementObject currentMObject = new ManagementObject(); string strWQuery = "SELECT DeviceID,ProductName,Description,NetEnabled " + "FROM Win32_NetworkAdapter " + "WHERE DeviceID = " + this.DeviceID.ToString(); ObjectQuery oQuery = new System.Management.ObjectQuery(strWQuery); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery); ManagementObjectCollection oReturnCollection = oSearcher.Get(); foreach (ManagementObject mo in oReturnCollection) { currentMObject = mo; } //Enable the Network Adapter currentMObject.InvokeMethod("Enable", null); //Disable the Network Adapter //currentMObject.InvokeMethod("Disable", null); 来源文章 MSDN 上的资源(Win32_NetworkAdapter 类说明)
使用 WMI 启用/禁用网络适配器
(CSWMIEnableDisableNetworkAdapter)
(针对 VS2010 设计和测试的示例)
1. NetworkAdapter 类构造函数
2. 此代码查找所有适配器。
3. 此代码打开和关闭网络适配器。
来源文章
MSDN 上的资源(Win32_NetworkAdapter 类说明)