1

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);
}
Bojan B
  • 2,091
  • 4
  • 18
  • 26
12shadow12
  • 307
  • 2
  • 10

1 Answers1

0

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");

Renato Afonso
  • 654
  • 6
  • 13