C# 添加修改防火牆端口及程序

文章轉自:http://sdfiyon.iteye.com/blog/1197511app

 

1、添加 COM 引用spa

在引用裏,選擇 COM 頁, 找到 NetFwTypeLib , 肯定便可code

2、添加容許經過防火牆的例外程序blog

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);

        }
    }
}
相關文章
相關標籤/搜索