public FWIfz() { int counter = 0; Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); INetFwRules Rules = fwPolicy2.Rules; IEnumerator rulesEnumerator = Rules.GetEnumerator(); foreach (INetFwRule rule in Rules) { String exePath = rule.ApplicationName as string; if (exePath != null && exePath.Length > 0) { if (rule.Action == 0) { if (!File.Exists(exePath)) { if (Regex.IsMatch(exePath, pattern)) { counter += 1; Console.WriteLine(exePath); fwPolicy2.Rules.Remove(rule.Name); } } } } } Console.WriteLine("-"); Console.WriteLine(counter + " rules deleted."); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public static List <Rule> GetFirewallRules(NET_FW_RULE_DIRECTION_ direction) { List <Rule> rules = new List <Rule>(); // Get firewall info INetFwPolicy2 fwPolicy = GetFirewallPolicy(); // Get firewall rules INetFwRules fwRules = fwPolicy.Rules; // Get rule enumerator IEnumerator enumerator = fwRules.GetEnumerator(); // For each firewall rule while (enumerator.MoveNext()) { INetFwRule fwRule = (INetFwRule)enumerator.Current; // If rule is not the direction we're looking for, skip if (fwRule.Direction != direction) { continue; } Rule rule = new Rule(fwRule.Name) { Description = checkString(fwRule.Description), ApplicationName = checkString(fwRule.ApplicationName), ServiceName = checkString(fwRule.serviceName), intProtocol = fwRule.Protocol, LocalPorts = checkString(fwRule.LocalPorts), RemotePorts = checkString(fwRule.RemotePorts), LocalAddresses = checkString(fwRule.LocalAddresses), RemoteAddresses = checkString(fwRule.RemoteAddresses), Enabled = fwRule.Enabled, Group = checkString(fwRule.Grouping), Profile = (NET_FW_PROFILE_TYPE2_)fwRule.Profiles, Action = fwRule.Action, }; rules.Add(rule); } return(rules); }