C#"灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED))"

时间:2017-08-07 15:32:59

标签: c#

我正在使用c#构建防火墙控制器。 我得到了:

  

灾难性故障(HRESULT异常:0x8000FFFF   (E_UNEXPECTED))

当我尝试从列表中添加防火墙规则时。当我创建规则并将其直接添加到防火墙时,它似乎运行良好。

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

0 个答案:

没有答案