添加/删除端口(或程序)到防火墙外
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;
}
}
}
/// 添加/删除端口(或程序)到防火墙例外,需要添加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;
}
}
}
相关文章推荐
- C#添加删除防火墙例外(程序、端口)
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables防火墙添加删除端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- C#(VB.NET)操作Windows自带的防火墙 之 添加/删除允许通过防火墙的例外程序
- Linux-iptables-防火墙-添加删除-端口
- Linux iptables 防火墙 添加删除 端口
- win7添加防火墙例外的程序和端口
- Linux iptables 防火墙 添加删除 端口 (转载)
- Linux iptables 防火墙 添加删除 端口
- 向防火墙中添加程序或端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口
- Linux iptables 防火墙 添加删除 端口 .
- C# 添加修改防火墙端口及程序