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; 
相關問題