编程方式启用/禁用连接 [英] Programmatically Enable / Disable Connection
问题描述
在Windows 7上我可以启用和通过网络连接管理器面板(系统设置)禁用连接。
on Windows 7 I can enable and disable connections via the Network Connections Manager panel (in system settings).
如何在C#中做到这一点编程?谢谢
How can I do this programmatically in C#? Thanks
推荐答案
您可以在C#中通过利用WMI和Win32_NetworkAdapter这WMI类实现这一目标。 。Win32_NetworkAdapter类有启用并可以选择的网络接口上执行禁用方法
You can achieve this in C# by leveraging WMI and the Win32_NetworkAdapter WMI class. The Win32_NetworkAdapter class has Enable and Disable methods which can be executed on a selected network interface.
用法的例子可以在这里找到:
An example of usage can be found here:
链接不可用,但在存档:
link not available, but archived at:
简单地说,步骤要做到这一点是:
Briefly, steps to do this are:
-
生成从VS类的包装命令提示符
Generate a wrapper for the class from VS command prompt
mgmtclassgen Win32_NetworkAdapter /L CS -p NetworkAdapter.cs
通过适配器步进:
Stepping through the adapters:
SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach(ManagementObject result in search.Get()) {
NetworkAdapter adapter = new NetworkAdapter(result);
// Identify the adapter you wish to disable here.
// In particular, check the AdapterType and
// Description properties.
// Here, we're selecting the LAN adapters.
if (adapter.AdapterType.Contains("Ethernet 802.3")) {
adapter.Disable();
}
}
这篇关于编程方式启用/禁用连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!