C#添加删除防火墙例外(程序、端口)
2018-01-04 17:29
477 查看
一、 添加 COM 引用
在引用里,选择 COM 页, 找到 NetFwTypeLib , 确定即可
二、 添加允许通过防火墙的例外程序
转载:http://sdfiyon.iteye.com/blog/1197511
在引用里,选择 COM 页, 找到 NetFwTypeLib , 确定即可
二、 添加允许通过防火墙的例外程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NetFwTypeLib; namespace Common { public static class INetFwManger { /// <summary> /// 添加防火墙例外端口 /// </summary> /// <param name="name">名称</param> /// <param name="port">端口</param> /// <param name="protocol">协议(TCP、UDP)</param> 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) netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort); } /// <summary> /// 将应用程序添加到防火墙例外 /// </summary> /// <param name="name">应用程序名称</param> /// <param name="executablePath">应用程序可执行文件全路径</param> 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); } /// <summary> /// 删除防火墙例外端口 /// </summary> /// <param name="port">端口</param> /// <param name="protocol">协议(TCP、UDP)</param> 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); } } /// <summary> /// 删除防火墙例外中应用程序 /// </summary> /// <param name="executablePath">程序的绝对路径</param> public static void NetFwDelApps(string executablePath) { INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath); } } }
转载:http://sdfiyon.iteye.com/blog/1197511
相关文章推荐
- C#(VB.NET)操作Windows自带的防火墙 之 添加/删除允许通过防火墙的例外程序
- [转]C#(VB.NET)操作Windows自带的防火墙 之 添加/删除允许通过防火墙的例外程序
- 添加/删除端口(或程序)到防火墙外
- C# 添加修改防火墙端口及程序
- win7添加防火墙例外的程序和端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- centos7 防火墙添加例外端口firewall
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables防火墙添加删除端口
- Windows Server 2012 防火墙如何添加端口例外的方法(转)
- C#把自写的卸载程序添加到控制面板的添加删除程序目录中
- windows 2008 server下设置防火墙的例外端口及程序
- c# 代码 添加或删除程序开机启动
- Linux iptables 防火墙 添加删除 端口 .
- C#把自写的卸载程序添加到控制面板的添加删除程序目录中
- Linux-iptables-防火墙-添加删除-端口