编程应用程序添加到的所有配置文件的Windows防火墙(Vista的+) [英] Programmatically add an application to all profile Windows Firewall (Vista+)
问题描述
我已搜查周围,也有类似的问题对SO,但没有一个人有关如何添加例外所有配置文件举行会谈(Windows 7中,又名任何个人资料VISTA / Windows Server 2008上)。 。在互联网上谈论举例加起来也只有当前配置文件
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.
这样做的原因是我有一个问题,我的虚拟机之一:Windows 2008 x86上,当前的防火墙配置文件域和我的应用程序被添加到域的例外列表中。 (防火墙设置为默认:阻止任何呼入电话不在例外列表中。)
然而,呼入呼叫仍然受阻,除非:
1.关闭防火墙,该虚拟机上。
2.手动更改我的应用程序的规则配置文件为任何
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"
这是非常令人困惑,因为我以为只有活动配置应该是积极的,并应功能,不管其他配置文件挡住了我的申请呼入电话。
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.
我使用XPSP2 INetFwMgr界面添加这是缺乏任何文件支持例外。
I am using XPSP2 INetFwMgr interface to add exceptions which is lacking of "any" profile support.
我使用C#但是例如任何语言可以理解的。
I am using c# but any language with example will be appreciated.
推荐答案
您可以尝试这样的事:
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);
}
}
}
有关着想完整性,添加引用到c:\Windows\System32\FirewallAPI.dll
For sake of completeness, add reference to c:\Windows\System32\FirewallAPI.dll
这篇关于编程应用程序添加到的所有配置文件的Windows防火墙(Vista的+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!