C#控制系统防火墙

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); } }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值