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

标签 c# networking adapter windows-firewall

鉴于 - .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 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;
    }
}

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

相关文章:

c# - 为什么在当前类是子类时对方法或属性使用 sealed

linux - 释放 TCP/IP 端口?

java - 使用捕获的数据包从服务器向应用程序发送 http 响应

c++ - 确定网络连接链接速度

http - 无法在独立适配器中终止 BizTalk 实例

c# - Async TCP Server 代码示例分析

c# - 客户端兼容性检查管理

检查测试条件值 IN 的 C# if 语句(就像在 t-sql 中一样)

android - java.lang.IllegalMonitorStateException : object not locked by thread before notify() - onResponse 异常

php - zend 新手问题 - "No adapter found for "