gpt4 book ai didi

用于测试网络适配器是否受防火墙保护的 C# API

转载 作者:太空宇宙 更新时间:2023-11-03 18:49:09 25 4
gpt4 key购买 nike

鉴于 -.Net 2.0带有 SP2 和多个网络适配器

是否有可用于检查网络适配器是否受防火墙保护的 API?

OneGuyInDC

最佳答案

试试下面的 C# 代码。它适用于 Windows 7(和 Vista)和 XP。这将获取当前配置文件的状态并启用/禁用 Windows 防火墙,例如:家庭/域/公共(public)访问网络。

用法:

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 methodsisWinXP()  --> 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;    }}

关于用于测试网络适配器是否受防火墙保护的 C# API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1663960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com