2010-06-16 95 views
9

我可以通過「網絡連接管理器」面板(在系統設置中)啓用和禁用連接。在Windows 7上以編程方式啓用/禁用連接

如何在C#中以編程方式執行此操作?謝謝

+0

任何特別的原因,因爲你顯然在計算機上停止所有流量,中斷下載等等? – 2010-06-16 12:53:26

回答

15

您可以通過利用WMI和Win32_NetworkAdapter WMI類在C#中實現此目的。 Win32_NetworkAdapter類具有啓用和禁用可在選定網絡接口上執行的方法。

用法的例子可以在這裏找到:

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

鏈接不可用,但在存檔:

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

簡而言之,步驟要做到這一點是:

  1. 產生由VS類的包裝命令提示符

    mgmtclassgen Win32_NetworkAdapter /L CS -p NetworkAdapter.cs
  2. 通過適配器步進:

     
    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(); 
    } 
    }

+0

啊試試Win10 UWP哈哈 – Joe 2015-09-16 17:13:44

相關問題