How to programmatically disable a network adapter (or reset) in C #

I need to disable a network adapter programmatically using C # (.NET 2.0) in Windows XP Embedded.

Background reason . After installing the Bluetooth stack on the PC, the Bluetooth PAN adapter blocks the Bluetooth manager program (which works in the system tray). If I turn off the Bluetooth PAN, then the Bluetooth manager is working fine.

This issue only occurs on computers running Windows XP Embedded.

+4
source share
4 answers

try the following:

netsh interface set interface "YOUR_ADAPTOR" DISABLED 
+2
source

netsh interface set interface "YOUR_ADAPTOR" DISABLED

NOTE: Pay attention to XP, but on Windows Vista / Windows 7 this will only work on a command line that runs as administrator (the option "Run as administrator").

+2
source

If you want to use the name specified in the device manager, it will probably be easier to use WMI. Request

 SELECT * FROM Win32_NetworkAdpater WHERE NName='name from device mnanager' 

will select a WMI object with Disable .

Something like this with the device name "Realtek PCIe GBE Family Controller":

 var searcher = new ManagementObjectSearcher("select * from win32_networkadapter where Name='Realtek PCIe GBE Family Controller'"); var found = searcher.Get(); var nicObj = found.First() as ManagementObject; // Need to cast from ManagementBaseObject to get access to InvokeMethod. var result = (uint)nicObj.InvokeMethod("Disable"); // 0 => success; otherwise error. 

NB. for example, Netsh will require elevation to disable this (but not to request).

+2
source

It depends on what you are trying to disable. If you are trying to disable LAN network interfaces, the only way on XP machines (as far as I know) to do this programmatically is to use devcon.exe (a program that looks like a device manager command line utility).

The syntax will be

 devcon disable *hardware ID of your adapter* 

You get an HWID (along with many other details) with

 wmic NIC 

or if you have access to Powershell on your XP machine, you can use this because there you can easily filter. wmic NIC does nothing but output Select * From Win32_NetworkAdapter

 gwmi win32_networkAdapter | select Name, PNPDeviceID | where {$_.Name -eq "*your adapter name*"} 

or

 gwmi -query "select Name, PNPDeviceID from Win32_Networkadapter" | where {$_.Name -eq "*your adapter name*"} 

The problem with using WMI to disable or enable your adapters is that the device driver must use the Disable() and Enable() methods, so you cannot rely on it to work.

I don’t know how well netsh works for bluetooth adapters and other devices, but I definitely recommend that you try this because it is a lot easier solution than using devcon and having to search for HWID.

0
source

All Articles