Programmatically add an application to all profile Windows Firewall (Vista+)

感情迁移 提交于 2019-12-04 05:49:47

You may try something like this:

using System;
using NetFwTypeLib;

namespace FirewallManager

{
  class Program
  {
    static void Main(string[] args)
    {
        INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
        firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
        firewallRule.Description = "Allow notepad";
        firewallRule.ApplicationName = @"C:\Windows\notepad.exe";
        firewallRule.Enabled = true;
        firewallRule.InterfaceTypes = "All";
        firewallRule.Name = "Notepad";

        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
        firewallPolicy.Rules.Add(firewallRule);

    }
  }
}

For sake of completeness, add reference to c:\Windows\System32\FirewallAPI.dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!