C#控制系統防火牆

來源:互聯網
上載者:User

using System;using System.Collections.Generic;using System.Text;using NetFwTypeLib;namespace FireWallTest{ public class FireWallHelp { /// /// 添加防火牆例外連接埠 /// /// 名稱 /// 連接埠 /// 協議(TCP、UDP) public static void NetFwAddPorts(string name, int port, string protocol)
{ //建立firewall管理類的執行個體 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); objPort.Name = name; objPort.Port
= port; if (protocol.ToUpper() == "TCP") { objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; } else { objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; } objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; objPort.Enabled = true; bool
exist = false; //加入到防火牆的管理原則 foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts) { if (objPort == mPort) { exist = true; break; } } if (exist) { System.Windows.Forms.MessageBox.Show("exist"); } if (!exist) netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort);
} /// /// 將應用程式添加到防火牆例外 /// /// 應用程式名稱 /// 應用程式可執行檔全路徑 public static void NetFwAddApps(string name, string executablePath) { //建立firewall管理類的執行個體 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); INetFwAuthorizedApplication
app = (INetFwAuthorizedApplication)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication")); //在例外列表裡,程式顯示的名稱 app.Name = name; //程式的路徑及檔案名稱 app.ProcessImageFileName = executablePath; //是否啟用該規則 app.Enabled = true; //加入到防火牆的管理原則 netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
bool exist = false; //加入到防火牆的管理原則 foreach (INetFwAuthorizedApplication mApp in netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications) { if (app == mApp) { exist = true; break; } } if (!exist) netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
} /// /// 刪除防火牆例外連接埠 /// /// 連接埠 /// 協議(TCP、UDP) public static void NetFwDelApps(int port, string protocol) { INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); if (protocol == "TCP") { netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port,
NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); } else { netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP); } } /// /// 刪除防火牆例外中應用程式 /// /// 程式的絕對路徑 public static void NetFwDelApps(string executablePath)
{ INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath); } }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.