编程方式启用/禁用连接 [英] Programmatically Enable / Disable Connection

查看:152
本文介绍了编程方式启用/禁用连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:

http://blog.opennetcf.com/ncowburn/ 2008/06/24 / HOWTODisableEnableNetworkConnectionsProgrammaticallyUnderVista.aspx

链接不可用,但在存档:

link not available, but archived at:

HTTP:/ /web.archive.org/web/20120615012706/http://blog.opennetcf.com/ncowburn/2008/06/24/HOWTODisableEnableNetworkConnectionsProgrammaticallyUnderVista.aspx

简单地说,步骤要做到这一点是:

Briefly, steps to do this are:


  1. 生成从VS类的包装命令提示符

  1. 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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆