如何检测防火墙产品是否已启用?

时间:2012-12-10 05:06:37

标签: c# windows-applications windows-firewall windows-firewall-api

如果启用了防火墙产品,我如何检测(来自用C#编写的Windows窗体应用程序)?

这是我的代码,我在 INetFwMgr上遇到错误,找不到类型或命名空间

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; 

       INetFwMgr manager = GetFireWallManager();
       bool isFirewallEnabled = manager.LocalPolicy.CurrentProfile.FirewallEnabled;



       private static INetFwMgr GetFireWallManager()
       {
           Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER));
           return Activator.CreateInstance(objectType) as INetFwMgr;
       }
        private void button1_Click(object sender, EventArgs e)
        {



            if (isFirewallEnabled == false)
           {
                MessageBox.Show("Firewall is not enabled.");
           }
           else
           {
                MessageBox.Show("Firewall is enabled.");
           }

        }
    }
}

6 个答案:

答案 0 :(得分:3)

NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;

有关详细信息,请参阅链接。

http://technet.microsoft.com/en-us/library/cc737845%28WS.10%29.aspx

http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx

答案 1 :(得分:2)

在此处查看有关防病毒How to detect antivirus installed on windows 2003 server and 2008 server 2003 server R2and 2008 server R2 using WMI or other then WMI in C++的此问题,可以使用相同的API调用来使用WSC_SECURITY_PROVIDER_FIREWALL枚举来检测防火墙设置。这个问题的答案实际上是错误的,但它会为您提供非服务器计算机的答案。该代码使用的是C ++,但它只是您需要的Windows API调用,您也可以从C#调用它。

答案 2 :(得分:1)

您首先需要将以下组件添加到项目中

  • INetFwMgr

然后,从家庭网络配置管理器CLSID获取对象类型{304CE942-6E39-40D8-943A-B913C40C9CD4}(链接到C:\WINDOWS\system32\hnetcfg.dll并且可以在HKEY_CLASSES_ROOT\CLSID\{304CE942-6E39-40D8-943A-B913C40C9CD4}找到)并使用收集的类型创建使用类型的默认构造函数作为新INetFwMgr的实例,它将用于检测防火墙是否已启用,使用INetFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled返回bool

private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; //This is the CLSID of Home Networking Configuration Manager. We'll use this to detect whether the Firewall is enabled or not
private static NetFwTypeLib.INetFwMgr GetHNCMType()
{
    Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER)); //Creates a new GUID from CLSID_FIREWALL_MANAGER getting its type as objectType
    return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr; //Creates an instance from the object type we gathered as an INetFwMgr object
}
static void Main(string[] args)
{
    INetFwMgr manager = GetHNCMType(); //Initializes a new INetFwMgr of name manager from GetHNCMType
    if (manager.LocalPolicy.CurrentProfile.FirewallEnabled == false) //Continue if the firewall is not enabled
    {
        //The firewall is not enabled
        Console.WriteLine("OFF"); //Writes OFF to the Console in a new line
    }
    else //Otherwise:
    {
        //The fire wall is enabled
        Console.WriteLine("ON"); //Writes ON to the Console in a new line
    }
}

谢谢, 我希望你觉得这很有帮助:)


要向项目添加组件,

  • 右键点击您的解决方案资源管理器下的参考 项目名称并选择添加参考...
  • 在标签 COM 下,选择您要添加的组件,然后点击确定

答案 3 :(得分:1)

我知道这是一个很老的帖子,但我找到了一个很好的解决方案! 阅读以下网址中的防火墙状态注册表项:

  

HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Services \ SharedAccess \ Parameters \ FirewallPolicy \ StandardProfile

键:EnableFirewall

public static bool isFirewallEnabled() {
        try {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile")) {
                if (key == null) {
                    return false;
                } else { 
                    Object o = key.GetValue("EnableFirewall");
                    if (o == null) {
                        return false;
                    } else {
                        int firewall = (int)o;
                        if (firewall == 1) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
            }
        } catch {
            return false;
        }
    }

您还可以获取DomainProfile,PublicProfile和StandardProfile的值。你也可以获得FirewallRules。

我希望这会有所帮助:)

答案 4 :(得分:1)

只需从C://windows/system32/hnetcfg.dll和C://windows/system32/FirewallAPI.dll

导入引用

然后使用

using NATUPNPLib;
using NETCONLib;
using NetFwTypeLib;

答案 5 :(得分:0)

您可以在旧版Windows(XP)中使用FwMgr,在Vista及以上版本中使用Windows Firewall with Advanced Security API

以下是检索防火墙设置的example