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

    /// <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;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/vs920079469vs/article/details/79245721
今日推荐