如何在 C# 中以编程方式禁用(或重置)网络适配器
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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).
试试这个:
try this:
如果您想使用设备管理器中显示的名称,那么使用 WMI 可能会更容易。查询
将选择 WMI 对象 与
禁用
< /a> 方法。类似这样的设备名称为“Realtek PCIe GBE 系列控制器”:
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
will select a WMI object with a
Disable
method.Something like this given a device name "Realtek PCIe GBE Family Controller":
NB. like
Netsh
this will require elevation to perform the disable (but not for the query).这取决于您想要禁用的内容。如果您尝试禁用 LAN 网络接口,那么在 XP 机器上(据我所知)以编程方式执行此操作的唯一可能性是使用 devcon.exe(类似于设备管理器命令行的程序)公用事业)。
语法是
You get the HWID (连同许多其他详细信息) with
或者 如果您可以在 XP 计算机上访问 Powershell,则可以使用它,因为您可以在那里很好地过滤 ir。
wmic NIC
除了输出Select * From Win32_NetworkAdapter
的结果或
使用 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
You get the HWID (along with many other details) with
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 ofSelect * From Win32_NetworkAdapter
or
The problem with using WMI to disable or enable your adapters is that it is up to the device driver to implement the
Disable()
andEnable()
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.