您的位置:首页 > 其它

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

2018-02-03 11:27 295 查看
    /// <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.F
4000
wAuthorizedApplication"));

                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;

            }

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: