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

4

6

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

Background Reason: After installing a Bluetooth stack on the PC, the Bluetooth PAN adapter blocks the Bluetooth manager program (that runs in the system tray). If I disable the Bluetooth PAN then the Bluetooth manager works fine.

This issue is happening only on Windows XP Embedded machines.

Limeade answered 19/9, 2011 at 10:14 Comment(1)
I remember looking for options to do that in Windows 7 when network drivers weren't really working out too great on my laptop. My research then found that this was impossible if it is set to get the IP automatically (i.e., dynamic address). I believe it's the same for XP, just so you know.Sachsse
G
3

netsh interface set interface "YOUR_ADAPTOR" DISABLED

NOTE: Note sure about XP, but in Windows Vista / Windows 7, this will only work on a command prompt run with administrator privileges ("Run as Administrator" option).

Gee answered 29/4, 2012 at 9:44 Comment(0)
V
2

try this:

netsh interface set interface "YOUR_ADAPTOR" DISABLED
Vowell answered 19/9, 2011 at 10:23 Comment(3)
"YOUR_ADAPTOR" here is the name as seen in the Device manager? or the instance id ??. Because i receive the error "An interface with this name is not registered with the router" upon executing your suggestionLimeade
@this-Me: I think it is the name, refer to the output of netsh interface show interface.Dirichlet
I ran "netsh interface show interface" command to get the interface name as "Local Area Connection 3". After this i tried running " ...netsh interface set interface "Local Area Connection 3" DISABLED.." command to receive errorLimeade
O
2

If you want to use the name shown in device manager it is probably going to be easier to use WMI. A query

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

will select a WMI object with a Disable method.

Something like this given a 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. like Netsh this will require elevation to perform the disable (but not for the query).

Onomatopoeia answered 29/4, 2012 at 10:51 Comment(1)
I think you have to replace the third line like this var nicObj = found.Cast<ManagementObject>().First();Shod
R
1

It depends on what you are trying to disable. If you are trying to disable LAN network interfaces then the only possibility on XP-machines (as far as I know) to do this programatically is using devcon.exe (a program that is like the device manager commandlline utility).

The syntax would be

devcon disable *hardware ID of your adapter*

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

wmic NIC

or if you have access to Powershell on your XP-machine you could use that because you can filter ir nicely there. wmic NIC does nothing else than output the results of 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 it is up to the device driver to implement the Disable() and Enable() Methods so you cant really rely on it working.

I dont know how well netsh works for bluetooth adapters and other devices but I would definitely recommend you try that because it's much simpler of a solution than using devcon and having to look up the HWID.

Resound answered 19/2, 2013 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.