如何使用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();
但是它只会给您例外。
答案 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;