7

如何检测(从用C#编写的Windows Forms应用程序)防火墙产品是否已启用?如何检测防火墙产品是否启用?

这里是我的代码和我得到的错误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."); 
      } 

     } 
    } 
} 
+0

你是否缺少using指令? – CRoshanLG

+0

是的。如何解决这个问题? –

+0

将命名空间Microsoft.TeamFoundation.Common添加到您的代码中。看到我的答案中的增加。 – CRoshanLG

回答

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#调用它。

+0

请注意,这只会检测本地运行的防火墙应用程序。它不会(也不能)检测防火墙设备等。 – Donnie

+0

不,它不会但给了这个问题上的标签(Windows防火墙等)我假设OP意味着本地防火墙 –

+0

也许,我只是想完成。 – Donnie

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

+0

感谢它对我有效 –

+0

请参阅下面的Jeetendra negi回答如何获取此代码进行编译。 –

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 
    } 
} 

谢谢,
我希望对您有所帮助:)


若要将组件添加到项目中,

  • 权-click 参考文献 from 解决方案资源管理器下你的 项目名称并选择添加引用...
  • 选项卡下COM,选择你想添加并单击OK
+1

我没有在COM下找到INetFwMgr。现在我怎样才能添加这个组件? –

+0

错误无法找到类型或名称空间名称'INetFwMgr'(缺少使用指令或程序集引用吗?)\t c:\ users \ administrator.mustuspune \ documents \ visual studio 2010 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1中。cs \t 我收到此错误 –

1

我知道这是组件一个旧帖子,但我找到了一个很好的解决方案
阅读中发现的防火墙状态的注册表项:

HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \服务\ SharedAccess \参数\ 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。

我希望这有助于:)

1

只是由C导入refrences://windows/system32/hnetcfg.dll和C://windows/system32/FirewallAPI.dll

然后使用

using NATUPNPLib; 
using NETCONLib; 
using NetFwTypeLib; 
相关问题