找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 398|回复: 0

[工具类] C#添加删除(程序、端口)防火墙例外

[复制链接] IP属地:广东省广州市
发表于 2023-6-7 23:05:05 | 显示全部楼层 |阅读模式
C#添加删除防火墙例外(程序、端口)
一、 添加 COM 引用
在引用里,选择 COM 页, 找到 NetFwTypeLib , 确定即可
二、 添加允许通过防火墙的例外程序
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NetFwTypeLib;
  6.    
  7. namespace Common
  8. {
  9.     public static class INetFwManger
  10.     {
  11.         /// <summary>
  12.         /// 添加防火墙例外端口
  13.         /// </summary>
  14.         /// <param name="name">名称</param>
  15.         /// <param name="port">端口</param>
  16.         /// <param name="protocol">协议(TCP、UDP)</param>
  17.         public static void NetFwAddPorts(string name, int port, string protocol)
  18.         {
  19.             //创建firewall管理类的实例
  20.             INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  21.    
  22.             INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance(
  23.                 Type.GetTypeFromProgID("HNetCfg.FwOpenPort"));
  24.    
  25.             objPort.Name = name;
  26.             objPort.Port = port;
  27.             if (protocol.ToUpper() == "TCP")
  28.             {
  29.                 objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
  30.             }
  31.             else
  32.             {
  33.                 objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
  34.             }
  35.             objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
  36.             objPort.Enabled = true;
  37.    
  38.             bool exist = false;
  39.             //加入到防火墙的管理策略
  40.             foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts)
  41.             {
  42.                 if (objPort == mPort)
  43.                 {
  44.                     exist = true;
  45.                     break;
  46.                 }
  47.             }
  48.             if (!exist) netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort);
  49.         }
  50.         /// <summary>
  51.         /// 将应用程序添加到防火墙例外
  52.         /// </summary>
  53.         /// <param name="name">应用程序名称</param>
  54.         /// <param name="executablePath">应用程序可执行文件全路径</param>
  55.         public static void NetFwAddApps(string name, string executablePath)
  56.         {
  57.             //创建firewall管理类的实例
  58.             INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  59.    
  60.             INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(
  61.                 Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
  62.    
  63.             //在例外列表里,程序显示的名称
  64.             app.Name = name;
  65.    
  66.             //程序的路径及文件名
  67.             app.ProcessImageFileName = executablePath;
  68.    
  69.             //是否启用该规则
  70.             app.Enabled = true;
  71.    
  72.             //加入到防火墙的管理策略
  73.             netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
  74.    
  75.             bool exist = false;
  76.             //加入到防火墙的管理策略
  77.             foreach (INetFwAuthorizedApplication mApp in netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications)
  78.             {
  79.                 if (app == mApp)
  80.                 {
  81.                     exist = true;
  82.                     break;
  83.                 }
  84.             }
  85.             if (!exist) netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
  86.         }
  87.         /// <summary>
  88.         /// 删除防火墙例外端口
  89.         /// </summary>
  90.         /// <param name="port">端口</param>
  91.         /// <param name="protocol">协议(TCP、UDP)</param>
  92.         public static void NetFwDelApps(int port,string protocol)
  93.         {
  94.             INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  95.             if (protocol == "TCP")
  96.             {
  97.                 netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
  98.             }
  99.             else
  100.             {
  101.                 netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
  102.             }
  103.         }
  104.         /// <summary>
  105.         /// 删除防火墙例外中应用程序
  106.         /// </summary>
  107.         /// <param name="executablePath">程序的绝对路径</param>
  108.         public static void NetFwDelApps(string executablePath)
  109.         {
  110.             INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  111.    
  112.             netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath);
  113.    
  114.         }
  115.     }
  116. }
复制代码






您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|西兴社区 ( 蜀ICP备2022005627号 )|网站地图

GMT+8, 2024-9-17 04:18 , Processed in 0.619057 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表