如何在 C# 中以编程方式禁用(或重置)网络适配器

发布于 2024-12-05 10:18:32 字数 207 浏览 0 评论 0原文

我需要在 Windows XP Embedded 上使用 C# (.NET 2.0) 以编程方式禁用网络适配器。

背景原因:在 PC 上安装蓝牙堆栈后,蓝牙 PAN 适配器会阻止蓝牙管理器程序(在系统托盘中运行)。如果我禁用蓝牙 PAN,则蓝牙管理器可以正常工作。

此问题仅发生在 Windows XP Embedded 计算机上。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你的手,一向走下去 2024-12-12 10:18:32

netsh interface set interface "YOUR_ADAPTOR" DISABLED

注意:请注意 XP,但在 Windows Vista / Windows 7 中,这仅适用于以管理员权限运行的命令提示符( “以管理员身份运行”选项)。

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).

つ可否回来 2024-12-12 10:18:32

试试这个:

netsh interface set interface "YOUR_ADAPTOR" DISABLED

try this:

netsh interface set interface "YOUR_ADAPTOR" DISABLED
筑梦 2024-12-12 10:18:32

如果您想使用设备管理器中显示的名称,那么使用 WMI 可能会更容易。查询

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

将选择 WMI 对象禁用< /a> 方法。

类似这样的设备名称为“Realtek PCIe GBE 系列控制器”:

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。与 Netsh 类似,这将需要提升才能执行禁用(但不适用于查询)。

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).

池予 2024-12-12 10:18:32

这取决于您想要禁用的内容。如果您尝试禁用 LAN 网络接口,那么在 XP 机器上(据我所知)以编程方式执行此操作的唯一可能性是使用 devcon.exe(类似于设备管理器命令行的程序)公用事业)。

语法是

devcon disable *hardware ID of your adapter*

You get the HWID (连同许多其他详细信息) with

wmic NIC

或者 如果您可以在 XP 计算机上访问 Powershell,则可以使用它,因为您可以在那里很好地过滤 ir。 wmic NIC 除了输出 Select * From Win32_NetworkAdapter 的结果

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

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

使用 WMI 禁用或启用适配器的问题在于,它取决于设备驱动程序实现 Disable()Enable() 方法,因此您不能真正依赖它工作。

我不知道 netsh 对于蓝牙适配器和其他设备的效果如何,但我绝对建议您尝试一下,因为它比使用 devcon 和查找 HWID 的解决方案简单得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文