很多時(shí)候,我們的程序是通過網(wǎng)絡(luò)通信(如TCP或者UDP協(xié)議+端口),而將制作好的程序安裝包給客戶用時(shí),發(fā)現(xiàn)會出現(xiàn)不能通信的現(xiàn)象(或者在這臺電腦是可以的,卻在另一臺不可以),原因是防火墻阻止了,需要添加防火墻例外?,F(xiàn)在將代碼記錄下來,方便以后備用。
在Visual studio 項(xiàng)目引用右鍵里面添加引用,選中COM然后找到NetFwTypeLib,確認(rèn),然后新建類FireWallHelp.cs添加
using NetFwTypeLib;
代碼如下
using System;
using System.Collections.Generic;
using System.Text;
using NetFwTypeLib;
namespace FireWallTest
{
public class FireWallHelp
{
///
/// 添加防火墻例外端口
///
/// 名稱
/// 端口
/// 協(xié)議(TCP、UDP)
public static void NetFwAddPorts(string name, int port, string protocol)
{
//創(chuàng)建firewall管理類的實(shí)例
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);
}
///
/// 將應(yīng)用程序添加到防火墻例外
///
/// 應(yīng)用程序名稱
/// 應(yīng)用程序可執(zhí)行文件全路徑
public static void NetFwAddApps(string name, string executablePath)
{
//創(chuàng)建firewall管理類的實(shí)例
INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
//在例外列表里,程序顯示的名稱
app.Name = name;
//程序的路徑及文件名
app.ProcessImageFileName = executablePath;
//是否啟用該規(guī)則
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);
}
///
/// 刪除防火墻例外端口
///
/// 端口
/// 協(xié)議(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);
}
}
///
/// 刪除防火墻例外中應(yīng)用程序
///
/// 程序的絕對路徑
public static void NetFwDelApps(string executablePath)
{
INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath);
}
}
}
該文章在 2019/9/6 10:00:03 編輯過