2009-11-02 5 views

Répondre

2

Il est impossible de savoir en général (par exemple, s'il y a un pare-feu externe) pour les raisons suivantes:

  1. Si vous ne recevez pas les connexions entrantes, votre externe interface peut-être juste en panne.
  2. Si vous ne parvenez pas à établir des connexions sortantes, il se peut que votre interface externe soit simplement hors service.

Mais il est une API pour savoir si le Pare-feu Windows est activé sur une interface de réseau donné. Vous devrez utiliser COM interop pour obtenir les interfaces INetFwProfile (pour l'état du pare-feu global) et INetSharingConfiguration (pour une interface réseau spécifique), et vérifier INetFwProfile.FirewallEnabled et INetSharingConfiguration.InternetFirewallEnabled. Voir http://msdn.microsoft.com/en-us/library/aa364717%28VS.85%29.aspx pour les liens et pour savoir comment utiliser ces résultats pour déterminer l'état effectif du pare-feu. (Il est écrit en termes de VBScript mais devrait pouvoir être traduit en C#.)

+0

Réponses et +1 Fusion –

5

Donnez ce code C# ci-dessous. Cela fonctionne pour Windows 7 (& Vista) et XP. Ceci permet d'obtenir le statut de, et d'activer/désactiver le pare-feu Windows pour le profil actuel, par exemple: Accueil/Domaine/Réseaux d'accès public.

Utilisation:

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. 

Code:

 
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; 
    } 
} 
Questions connexes