鉴于 - .Net 2.0 XP机器与SP2和 多个网络适配器
是否有可用于检查网络适配器是否为防火墙的API?
OneGuyInDC
答案 0 :(得分:5)
将此c#代码放在下面。它适用于Windows 7(& Vista)和XP。 这将获取当前配置文件的状态,并启用/禁用Windows防火墙,例如:Home / Domain / Public access networks。
<强>用法:强>
getFirewallStatus() --> returns true/false for whether the windows firewall is enable/disabled. setFirewallStatus(newStatus) --> sets the firewall enabled/disabled to the true/false value passed in eg, to enable the firewall: setFirewallStatus(true) getCurrPolicy() --> used by the other two methods isWinXP() --> returns whether windows version is WinXP/2000 or newer, ie: Vista/Win7 used by the other methods to determine which code to use.
代码:
using NetFwTypeLib; // (don't forget to add it to your references, its under the COM tab) public bool isWinXP() { OperatingSystem os = Environment.OSVersion; int majorVersion = os.Version.Major; // see http://msdn.microsoft.com/en-us/library/ms724832(v=vs.85).aspx if (majorVersion < 6) // if O/S is not Vista or Windows7 { return true; } else { return false; } } private static INetFwPolicy2 getCurrPolicy() { INetFwPolicy2 fwPolicy2; Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); return fwPolicy2; } public bool getFirewallStatus() { bool result = false; switch (isWinXP()) { case true: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); result = mgr.LocalPolicy.CurrentProfile.FirewallEnabled; break; case false: 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)); break; default: result = false; // assume Win7 by default break; } return result; } public void setFirewallStatus(bool newStatus) { switch (isWinXP()) { case true: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); mgr.LocalPolicy.CurrentProfile.FirewallEnabled = newStatus; break; case false: 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); break; default: NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes1; INetFwPolicy2 currPolicy1 = getCurrPolicy(); //read Current Profile Types (only to increase Performace) //avoids access on CurrentProfileTypes from each Property fwCurrentProfileTypes1 = (NET_FW_PROFILE_TYPE2_)currPolicy1.CurrentProfileTypes; currPolicy1.set_FirewallEnabled(fwCurrentProfileTypes1, newStatus); break; } }
答案 1 :(得分:2)
由于以下原因,无法知道一般(例如,如果有外部防火墙):
但是一个API,用于查明是否在给定的网络接口上启用了Windows防火墙。您将需要使用COM interop来获取INetFwProfile(用于全局防火墙状态)和INetSharingConfiguration(用于特定网络接口)接口,并检查INetFwProfile.FirewallEnabled和INetSharingConfiguration.InternetFirewallEnabled。
请参阅http://msdn.microsoft.com/en-us/library/aa364717%28VS.85%29.aspx以获取链接以及如何使用这些结果来确定有效的防火墙状态。 (它是用VBScript编写的,但应该可以翻译成C#。)