C#Netfwtypelib-添加多个1远程地址吗?

时间:2018-10-28 14:56:50

标签: c# firewall

如何使用C#向防火墙规则添加超过1个IP地址?

我试图做类似的事情:

rule.RemoteAddresses += "127.0.0.1";
rule.RemoteAddresses += "129.0.0.1";

List<string> list = new List<string>();
list.Add("127.0.0.1");
list.Add("129.0.0.1");
rule.RemoteAddresses = list.ToString();

但是它只会给您例外。

1 个答案:

答案 0 :(得分:0)

我在Programmatically add IP to Server 2008 firewall rule上找到了它

工作代码:

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
var rule = firewallPolicy.Rules.Item("Block Bad IP Addresses");

rule.RemoteAddresses += "," + ip;