I would like to get a specific firewall rule by name and see the options it uses (The IP Scope specifically) and compare it to something. Is this possible, I searched online but couldn't find anything.
This is how I am adding rules:
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
INetFwRule firewallRule = firewallPolicy.Rules.OfType<INetFwRule>().Where(x => x.Name == RULE_NAME).FirstOrDefault();
if (firewallRule == null)
{
firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Name = RULE_NAME;
/* More stuff */
firewallPolicy.Rules.Add(firewallRule);
}
Inside that object firewallPolicy.Rules, you might access and use a foreach loop to go for each rule you have inside your firewall. If you want some rule specifically, use linq to seach inside that collection: Something like this:
var rule = firewallPolicy.Rules.Where(n=> n.Name == "your name");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.