以编程方式将应用程序添加到所有配置文件 Windows 防火墙 (Vista+)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5641839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically add an application to all profile Windows Firewall (Vista+)
提问by Yuan
I have searched around and there are similar questions on SO, however, no one talks about how to add exception to "All Profile" (windows 7, AKA "Any Profile" on Vista/Windows Server 2008). Examples on internet talk about add to current profile only.
我四处搜索,在 SO 上也有类似的问题,但是,没有人谈论如何向“所有配置文件”(Windows 7,Vista/Windows Server 2008 上的“任何配置文件”)添加例外。互联网上的示例仅讨论添加到当前配置文件。
The reason for this is I have a problem with one of my virtual machine: windows 2008 x86, current firewall profile is Domain, and my application is added to Exception list of Domain. (Firewall setting is as default: block any inbound calls that are not in exception list.) However, inbound calls are still blocked unless : 1. turn off firewall on that this virtual machine. 2. manually change rule profile of my application to "any"
原因是我的一个虚拟机有问题:windows 2008 x86,当前的防火墙配置文件是域,我的应用程序被添加到域的例外列表中。(防火墙设置为默认设置:阻止任何不在例外列表中的入站呼叫。)但是,入站呼叫仍会被阻止,除非: 1. 关闭该虚拟机上的防火墙。2. 手动将我的应用程序的规则配置文件更改为“任何”
It is very confusing as I thought only active profile should be "active" and should be functional, no matter other profiles are blocking my application inbound calls.
这非常令人困惑,因为我认为只有活动配置文件应该是“活动的”并且应该是功能性的,无论其他配置文件是否阻止了我的应用程序入站调用。
I am using XPSP2 INetFwMgr interface to add exceptions which is lacking of "any" profile support.
我正在使用 XPSP2 INetFwMgr 接口来添加缺少“任何”配置文件支持的异常。
I am using c# but any language with example will be appreciated.
我正在使用 c#,但任何带有示例的语言都会受到赞赏。
回答by manojlds
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
为了完整起见,添加对 c:\Windows\System32\FirewallAPI.dll 的引用