C# 通过win7 FirewallAPI向专用和公用网络添加应用程序防火墙规则

C# 通过win7 FirewallAPI向专用和公用网络添加应用程序防火墙规则,c#,firewall,windows-firewall,windows-firewall-api,firewall-access,C#,Firewall,Windows Firewall,Windows Firewall Api,Firewall Access,一点背景:基本上,我想为私有和公共网络添加一个程序防火墙访问规则 我以前用这个- “netsh firewall add allowedprogram program=“Path..”name=AppName ENABLE scope=ALL profile=CURRENT” 但是现在我想使用COM对象稍微自动化一下这个过程。 找到了这段闪亮的代码- 在实现了我一直尝试使用的类之后- FirewallHelper.Instance.GrantAuthorization(@“路径…”,“应用程序名

一点背景:基本上,我想为私有和公共网络添加一个程序防火墙访问规则

我以前用这个- “netsh firewall add allowedprogram program=“Path..”name=AppName ENABLE scope=ALL profile=CURRENT”

但是现在我想使用COM对象稍微自动化一下这个过程。 找到了这段闪亮的代码-

在实现了我一直尝试使用的类之后- FirewallHelper.Instance.GrantAuthorization(@“路径…”,“应用程序名”、网络范围、网络范围、网络IP版本、网络IP版本)

我面临的问题是GrantAuthorization方法只会为公共或私有网络添加一个规则,而我以前的netsh命令会为每个网络添加两个规则-1

这些命令实际上看起来非常相似,所以对我来说有点吓人

所以。。。如何添加这两个网络规则


肖恩

我想你最好的办法是和警察谈谈

在谷歌上快速搜索“C#inetfrule2”将向您展示如何注册或更新防火墙规则的众多示例

为了增加公共和私人政策,我使用了一些

Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
var currentProfiles = fwPolicy2.CurrentProfileTypes;

// Let's create a new rule

INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
inboundRule.LocalPorts = "1234";
inboundRule.Protocol = 6; // TCP
// ...
inboundRule.Profiles = currentProfiles;

// Now add the rule

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

我的答案来自大卫的答案,但更详细。并修复有关设置本地端口的问题。在设置本地端口之前,需要先设置协议。详情如下:

首先,需要导入引用FirewallAPI.dll。它位于“C:\Windows\System32\FirewallAPI.dll”中 然后:

并将代码插入到您的:

        Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
        var currentProfiles = fwPolicy2.CurrentProfileTypes;

        // Let's create a new rule
        INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
        inboundRule.Enabled = true;
        //Allow through firewall
        inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
        //Using protocol TCP
        inboundRule.Protocol = 6; // TCP
        //Port 81
        inboundRule.LocalPorts = "81";
        //Name of rule
        inboundRule.Name = "MyRule";
        // ...//
        inboundRule.Profiles = currentProfiles;

        // Now add the rule
        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
        firewallPolicy.Rules.Add(inboundRule);

以防你们想要出站规则:


这页没有说这已经被回答了,而且是旧的,所以以防万一,为了将来的使用,我会回答这个问题

首先,导入位于“C:\Windows\System32\FirewallAPI.dll”的参考FirewallAPI.dll,然后添加using指令

using NetFwTypeLib;
inboundRule.Profiles
属性似乎被分类为一组具有以下值的标志(该属性的类型是int,因此我创建了一个枚举):

因此,使用该代码,我们可以将配置文件更改为以下内容:

// Create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
// Enable the rule
inboundRule.Enabled = true;
// Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
// Using protocol TCP
inboundRule.Protocol = 6; // TCP
// Set port number
inboundRule.LocalPorts = "1234";
// Name of rule
inboundRule.Name = "Name Of Firewall Rule";
// Set profiles
inboundRule.Profiles = (int)(FirewallProfiles.Private | FirewallProfiles.Public);

// Add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
或者您可以将
inboundRule.Profiles
更改为int值

注二:

1:如果您没有在管理权限下运行此代码

firewallPolicty.Rules.Add(inboundRule);
将引发异常

2:
inboundRule。配置文件必须介于值1和7之间。否则,它将抛出异常

public enum FirewallProfiles
{
    Domain = 1,
    Private = 2,
    Public = 4
}
// Create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
// Enable the rule
inboundRule.Enabled = true;
// Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
// Using protocol TCP
inboundRule.Protocol = 6; // TCP
// Set port number
inboundRule.LocalPorts = "1234";
// Name of rule
inboundRule.Name = "Name Of Firewall Rule";
// Set profiles
inboundRule.Profiles = (int)(FirewallProfiles.Private | FirewallProfiles.Public);

// Add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
firewallPolicty.Rules.Add(inboundRule);