SoFunction
Updated on 2025-03-07

c# Turn on or off the firewall through code

There are two ways to operate a firewall through code: one is to modify the registry to enable or close the firewall; the other is to directly operate the firewall object to enable or close the firewall. No matter which method, administrator permissions are required, so you need to determine whether the program has administrator permissions before operation.

1. Determine whether the program has administrator rights

Need to reference the namespace:

/// <summary>
/// Determine whether the program has administrator privileges/// </summary>
/// <returns>true: is an administrator; false: is not an administrator</returns>public static bool IsAdministrator()
{
  WindowsIdentity current = ();
  WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
  return ();
}

2. Registry modification firewall

Need to reference the namespace: Microsoft.Win32

/// &lt;summary&gt;
/// Operate the firewall through the registry/// &lt;/summary&gt;
/// <param name="domainState">Domain network firewall (disabled: 0; enable (default): 1)</param>/// <param name="publicState">Public network firewall (disable: 0; enable (default): 1)</param>/// <param name="standardState">Private network firewall (disable: 0; enable (default): 1)</param>/// &lt;returns&gt;&lt;/returns&gt;
public static bool FirewallOperateByRegistryKey(int domainState=1, int publicState = 1, int standardState = 1)
{
  RegistryKey key = ;
  try
  {
    string path = "HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\SharedAccess\\Defaults\\FirewallPolicy";
    RegistryKey firewall = (path, true);
    RegistryKey domainProfile = ("DomainProfile", true);
    RegistryKey publicProfile = ("PublicProfile", true);
    RegistryKey standardProfile = ("StandardProfile", true);
    ("EnableFirewall", domainState, );
    ("EnableFirewall", publicState, );
    ("EnableFirewall", standardState, );
  }
  catch (Exception e)
  {
    string error = $"registry modification error:{}";
    throw new Exception(error);
  }
  return true;
}

 3. Directly operate the firewall object

Need to add a reference to NetFwTypeLib in the project reference and reference the namespace NetFwTypeLib

/// &lt;summary&gt;
/// Operation through object firewall/// &lt;/summary&gt;
/// <param name="isOpenDomain">Domain network firewall (disable: false; enable (default): true)</param>/// <param name="isOpenPublicState">Public network firewall (disable: false; enable (default): true)</param>/// <param name="isOpenStandard">Private network firewall (disable: false; enable (default): true)</param>/// &lt;returns&gt;&lt;/returns&gt;
public static bool FirewallOperateByObject(bool isOpenDomain = true, bool isOpenPublicState = true, bool isOpenStandard = true)
{
  try
  {
    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)(("HNetCfg.FwPolicy2"));
    // Enable <Advanced Security Windows Firewall> - Firewall with proprietary configuration files    firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, isOpenStandard);
    // Enable <Advanced Security Windows Firewall> - Firewall for public configuration files    firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, isOpenPublicState);
    // Enable <Advanced Security Windows Firewall> - Firewall for Domain Configuration Files    firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN, isOpenDomain);
  }
  catch (Exception e)
  {
    string error = $"Firewall modification error:{}";
    throw new Exception(error);
  }
  return true;
}

The above is the detailed content of C# turning on or off the firewall through code. For more information about C# firewall, please follow my other related articles!