I am building a firewall controller using c#. I am getting a :
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
When I am trying to add firewall rules from a list. It seems to work out fine when I just create a rule and add it to the firewall directly.
public partial class Form1 : Form
{
private List<INetFwRule2> RuleList = new List<INetFwRule2>();
private Type netFwPolicy2Type = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
private INetFwPolicy2 fwPolicy2;
public Form1()
{
InitializeComponent();
fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(netFwPolicy2Type);
// Adding all fireWall rules in a List
foreach (INetFwRule2 rule in fwPolicy2.Rules)
{
RuleList.Add(rule);
}
}
private void ApplyButton_Click(object sender, EventArgs e)
{
// Deleting all firewall rules
foreach (INetFwRule2 rule in fwPolicy2.Rules)
{
fwPolicy2.Rules.Remove(rule.Name);
}
// Adding rules from the List to the windows firewall
INetFwRule2 newRule;
for (int i=0; i < RuleList.Count; i++)
{
newRule = RuleList[i];
try
{
fwPolicy2.Rules.Add(newRule);
}
catch (Exception r)
{
Console.WriteLine("Error adding rule firewall");
}
}
}
}
User contributions licensed under CC BY-SA 3.0