我能夠在文法上將單獨的規則添加到Windows防火牆(Server 2008 R2),但是我試圖避免每個IP地址有多個規則,並且只想更新現有規則RemoteAddresses 。以下是我用來添加規則的代碼,我正在盡力研究如何更新現有規則遠程地址,但沒有運氣。使用API更新現有的防火牆規則
任何幫助表示讚賞!
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);
請避免在C#或類似的東西,這是標籤的用途。 – 2012-03-15 01:41:43