6

We have an application which add some rules to firewall. We need to retrieve the rules of firewall so that we can check whether the rule exists in the firewall. I am using C#.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
User123
  • 373
  • 2
  • 7
  • 14
  • 1
    [Here](http://www.codeproject.com/Articles/19003/The-managed-classes-to-read-Windows-Firewall-confi) you can find an old article on reading the property settings of the Windows Vista Firewall. Don't know if it is still valid – Steve Apr 26 '12 at 22:35
  • Does this work for all the OS ???? – User123 Apr 27 '12 at 02:09

4 Answers4

4

Search the Firewall rules using a rule Name and remove it:

public static void RemoveFirewallRules(string RuleName)
{
    try
    {
        Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
        var currentProfiles = fwPolicy2.CurrentProfileTypes;               

        // List of rules
        // List<INetFwRule> RuleList = new List<INetFwRule>();

        foreach (INetFwRule rule in fwPolicy2.Rules)
        {
            // Add a rule to list
            // RuleList.Add(rule);
            // Console.WriteLine(rule.Name);
            if (rule.Name.IndexOf(RuleName) != -1)
            {
                // Remove a rule
                INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));                     
                firewallPolicy.Rules.Remove(rule.Name);
                Console.WriteLine(rule.Name + " has been deleted from the Firewall Policy");
            }
        }
    }
    catch (Exception r)
    {
        Console.WriteLine("Error deleting a Firewall rule");
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
2

If you dont want to add extra references to assemblies providing the INetFwPolicy2 interface, you can use the dynamic class in C# to reflect the "HNetCfg.FwPolicy2" type. See this working code:

Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
dynamic fwPolicy2 = Activator.CreateInstance(tNetFwPolicy2) as dynamic;                          
IEnumerable Rules = fwPolicy2.Rules as IEnumerable;
foreach (dynamic rule in Rules)
  {
  if (rule.Name=="My firewall rule")
    {

    }                              
  }

Note that using the dynamic object in this way requires a reference to Microsoft.CSharp assembly.

0

You can get an idea from this code.

INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);

List<INetFwRule> RuleList = new List<INetFwRule>();

foreach (INetFwRule rule in fwPolicy2.Rules)
{
  RuleList.Add(rule);
}
Achira
  • 1
  • 2
0

EDITED: INetFwPolicy2 is a .Net interface to manage Firewall rules msdn INetFwPolicy2 has a rules collection contains all ruls of this policy. You can query the Rules collection with Lambda, as follow:

INetFwPolicy2 fwPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
fwPolicy2.Rules.OfType<INetFwRule>.Select....

and all other Lambda extensions.

IFink
  • 724
  • 16
  • 28
  • You should really improve this answer by showing the type of fwPolicy2, preferably the fully qualified name, and preferably with a link to its documentation. – nos Dec 15 '14 at 13:27