C#(VB.NET)操作Windows內建的防火牆 之 啟用(開啟)/禁用(關閉)防火牆

來源:互聯網
上載者:User
文章目錄
  • 核心代碼:

在上一篇 C#(VB.NET)操作Windows內建的防火牆 之 綜述篇 裡,我們提到了下面這幅圖

本篇,我們就實現 啟用/關閉是否允許例外 的功能

先說明一點,因為windows內建了“資訊安全中心” Security Center 這個功能,用程式關閉防火牆時,“資訊安全中心” 會彈出提示,

要解決這個問題,其實很簡單,就是 將 Security Center 關閉,關閉的方法: 利用 net stop 或者 修改註冊表 或者.NET 裡面內建的 windows service操作,都可以,我就不囉嗦了

核心代碼:

C# 方式:

  /// <summary>  /// 擷取防火牆管理的當前策略  /// </summary>  /// <returns>返回策略對應的 object</returns>  private static object FirewallCurrentProfile()  {      //擷取管理防火牆的COM組件的type      Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", true);      //建立一個COM組件的執行個體      object fwMgr = Activator.CreateInstance(fwMgrType);      //擷取執行個體的LocalPolicy屬性      object localPolicy = fwMgrType.InvokeMember("LocalPolicy", BindingFlags.GetProperty, null, fwMgr, null);      //擷取 LocalPolicy屬性的子屬性CurrentProfile      return localPolicy.GetType().InvokeMember("CurrentProfile", BindingFlags.GetProperty, null, localPolicy, null);  }

VB.NET 方式:

    Private Function FirewallProfile() As Object        Dim FWMgr As Object = CreateObject("HNetCfg.FwMgr")        Return FWMgr.LocalPolicy.CurrentProfil    End Function

C# 版本原始碼下載                                  VB.NET版本原始碼(有需要的留言吧,懶得去寫了,嘿嘿)

原文:http://www.zu14.cn/2009/01/15/dotnet-enable-disable-firewall/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.