【发布时间】:2013-02-06 09:47:38
【问题描述】:
我正在开展一个项目,该项目必须能够通过程序启用/禁用 Windows 7 Embedded 系统的集成 Windows 防火墙。
使用代码:
private static INetFwPolicy2 getCurrPolicy()
{
INetFwPolicy2 fwPolicy2;
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
if (tNetFwPolicy2 != null)
fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
else
return null;
return fwPolicy2;
}
public static bool GetFirewallStatus()
{
bool result = false;
try
{
INetFwPolicy2 fwPolicy2 = getCurrPolicy();
NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes;
//read Current Profile Types (only to increase Performace)
//avoids access on CurrentProfileTypes from each Property
fwCurrentProfileTypes = NET_FW_PROFILE_TYPE2_)fwPolicy2.CurrentProfileTypes;
result = (fwPolicy2.get_FirewallEnabled(fwCurrentProfileTypes));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return result;
}
public static void SetFirewallStatus(bool newStatus)
{
try
{
NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes;
INetFwPolicy2 currPolicy = getCurrPolicy();
//read Current Profile Types (only to increase Performace)
//avoids access on CurrentProfileTypes from each Property
fwCurrentProfileTypes = NET_FW_PROFILE_TYPE2_)currPolicy.CurrentProfileTypes;
currPolicy.set_FirewallEnabled(fwCurrentProfileTypes, newStatus);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
发生的情况是,在 currPolicy 对象的每个函数调用中,我都会收到以下消息:
System.ArgumentException:值不在预期范围内。
现在我的问题是:为什么会出现此错误以及如何使其正常工作?
谢谢!
【问题讨论】:
-
为什么不以编程方式停止和禁用防火墙服务?
-
嗯,也已经想到了。可能是一种可能。但是如果有一个 API 让我有机会通过它设置防火墙设置,我会考虑使用它。由于它不起作用,我想知道是否有其他人也遇到过我的问题。
-
无论如何如果没有办法让它工作,我下一次尝试将使用命令行工具technet.microsoft.com/en-us/library/cc766337%28v=ws.10%29.aspx
-
公平点。那篇 TechNet 帖子看起来更有希望。
-
很遗憾,我无法使用 API 解决问题。无论如何,可以通过使用命令行工具使用我之前评论中的链接来实现打开或关闭防火墙
标签: c# windows range firewall argumentexception