Code:
Public Sub RemoveFirewallRules(ByVal Optional RuleName As String = "Test")
Try
Dim tNetFwPolicy2 As Type = Type.GetTypeFromProgID("HNetCfg.FwPolicy2")
Dim fwPolicy2 As INetFwPolicy2 = CType(Activator.CreateInstance(tNetFwPolicy2), INetFwPolicy2)
Dim currentProfiles = fwPolicy2.CurrentProfileTypes
Dim RuleList As List(Of INetFwRule) = New List(Of INetFwRule)()
Dim rule As INetFwRule
i = 0
For Each rule In fwPolicy2.Rules
If rule.Name.IndexOf(RuleName) <> -1 Then
Dim firewallPolicy As INetFwPolicy2 = CType(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")), INetFwPolicy2)
firewallPolicy.Rules.Remove(rule.Name)
i = i + 1
End If
Next
End
If i <> 0 Then
MsgBox(RuleName & " has been deleted from Firewall Policy")
End If
Catch r As Exception
MsgBox(r.Message)
End Try
End Sub
I have the code above that works on Windows 7 and above.
The code does not work with Windows XP. I also need to have the code in windows xp because we still have that operating system.
When I run the program on windows xp, it will throw error "VALUE CANNOT BE NULL. PARAMETER NAME: TYPE"
What I want to achieve is to delete unwanted or unauthorized firewall entries programmatically which I will be comparing to list of allowed firewall names on a text file.
I just need to delete it by firewall rule name and not by other way.
Hoping for someone to help.