更新现有的API使用防火墙规则 [英] Updating existing firewall rule using API

查看:862
本文介绍了更新现有的API使用防火墙规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能亲个体语法规则添加到Windows防火墙(Server 2008 R2中),但我想避免每个IP地址的多个规则,而只是想更新现有的规则RemoteAddresses。下面是code我使用补充规则,我尽我所能去研究如何更新现有规则的远程地址,但没有运气。

任何帮助AP preciated!

 字符串IP =X.X.X.X;INetFwRule2 firewallRule =(INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID(HNetCfg.FWRule));firewallRule.Name =阻止坏IP地址;
firewallRule.Description =从IP地址块讨厌传入的连接。
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.Enabled = TRUE;
firewallRule.InterfaceTypes =所有;
firewallRule.RemoteAddresses = IP;INetFwPolicy2 firewallPolicy =(INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID(HNetCfg.FwPolicy2));
firewallPolicy.Rules.Add(firewallRule);


解决方案

在code以下工作对我来说:

  INetFwPolicy2 firewallPolicy =(INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID(HNetCfg.FwPolicy2));VAR规则= firewallPolicy.Rules.Item(阻止坏IP地址); //你的规则名称这里
rule.Name =块块块; //此处更新规则。没有别的需要坚持的变化

I am able to pro grammatically add individual rules to the Windows Firewall (Server 2008 R2), however I am trying to avoid multiple rules per IP address, and would just like to update the existing rule RemoteAddresses. Below is the code I am using to add rules, I am doing my best to research how to update the existing rules Remote Addresses, but with no luck.

Any help is appreciated!

string ip = "x.x.x.x";

INetFwRule2 firewallRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));

firewallRule.Name = "Block Bad IP Addresses";
firewallRule.Description = "Block Nasty Incoming Connections from IP Address.";
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.RemoteAddresses = ip;

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);

解决方案

The code below works for me:

INetFwPolicy2 firewallPolicy = (INetFwPolicy2) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));

var rule = firewallPolicy.Rules.Item("Block Bad IP Addresses"); // Name of your rule here
rule.Name = "Block Block Block"; // Update the rule here. Nothing else needed to persist the changes

这篇关于更新现有的API使用防火墙规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆