添加/删除端口(或程序)到防火墙外

    /// <summary>
    /// 添加/删除端口(或程序)到防火墙例外,需要添加COM组件NetFwTypeLib引用
    /// </summary>
    public static class INetFwManger
    {
        /// <summary>
        /// 添加端口到防火墙例外
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="port">端口</param>
        /// <param name="protocol">The protocol.</param>
        /// <returns></returns>
        public static bool NetFwAddPort(string name, int port, NET_FW_IP_PROTOCOL_ protocol)
        {
            try
            {
                INetFwMgr netFwMgr = (INetFwMgr) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
                INetFwOpenPort objPort = (INetFwOpenPort) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort"));
                objPort.Name = name;
                objPort.Port = port;
                objPort.Protocol = protocol;
                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);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 将应用程序添加到防火墙例外
        /// </summary>
        /// <param name="name">应用程序名称</param>
        /// <param name="executablePath">应用程序可执行文件全路径</param>
        /// <returns></returns>
        public static bool NetFwAddApp(string name, string executablePath)
        {
            try
            {
                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.ProcessImageFileName == mApp.ProcessImageFileName)
                    {
                        exist = true;
                        break;
                    }
                }
                if (!exist)
                {
                    netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 删除防火墙例外端口
        /// </summary>
        /// <param name="port">端口</param>
        /// <param name="protocol">The protocol.</param>
        /// <returns></returns>
        public static bool NetFwDelPort(int port, NET_FW_IP_PROTOCOL_ protocol)
        {
            try
            {
                INetFwMgr netFwMgr = (INetFwMgr) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
                bool exist = false;
                foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts)
                {
                    if (mPort.Port == port)
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                {
                    netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, protocol);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 删除防火墙例外程序
        /// </summary>
        /// <param name="excutablePath">应用程序可执行文件全路径</param>
        /// <returns></returns>
        public static bool NetFwDelApp(string excutablePath)
        {
            try
            {
                INetFwMgr netFwMgr = (INetFwMgr) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
                bool exist = false;
                foreach (INetFwAuthorizedApplication mApp in netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications)
                {
                    if (mApp.ProcessImageFileName == excutablePath)
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                {
                    netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(excutablePath);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值