C#API测试网络适配器是否已防火墙 [英] C# API to test if a network Adapter is firewalled

查看:233
本文介绍了C#API测试网络适配器是否已防火墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出-
.net 2.0
带有SP2和
多个网络适配器的XP机器

Given - .Net 2.0 XP machine with SP2 and multiple network adapters

是否有一个可以

OneGuyInDC

OneGuyInDC

推荐答案

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

Give this c# code below a go. It works for Windows 7 (& Vista) and XP. This will get the status of, and enable/disable the Windows firewall for the current profile, eg: 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;
    }
}

这篇关于C#API测试网络适配器是否已防火墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆