QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

手机号码,快捷登录

查看: 2154|回复: 0

[C#/.NET] 通过代码开启或关闭防火墙C#版

[复制链接]

等级头衔

积分成就    金币 : 2857
   泡泡 : 1516
   精华 : 6
   在线时间 : 1317 小时
   最后登录 : 2025-4-23

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2020-10-29 11:49:07 | 显示全部楼层 |阅读模式
       通过代码操作防火墙的方式有两种:一是代码操作修改注册表启用或关闭防火墙;二是直接操作防火墙对象来启用或关闭防火墙。不论哪一种方式,都需要使用管理员权限,所以操作前需要判断程序是否具有管理员权限。* w& \9 f. o- V7 @3 S; f  v
1、判断程序是否拥有管理员权限# i, u# O" _0 G7 z
       需要引用命名空间:System.Security.Principal
7 I3 H# Y# u9 x/ e: Z$ t" o
/// <summary>
/// 判断程序是否拥有管理员权限
/// </summary>
/// <returns>true:是管理员;false:不是管理员</returns>
public static bool IsAdministrator()
{
  WindowsIdentity current = WindowsIdentity.GetCurrent();
  WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
  return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
2、注册表修改防火墙
, m5 n# ~8 ^* L9 X       需要引用命名空间:Microsoft.Win32# Z4 [$ ~9 _7 ?: G6 L! p
/// <summary>
/// 通过注册表操作防火墙
/// </summary>
/// <param name="domainState">域网络防火墙(禁用:0;启用(默认):1)</param>
/// <param name="publicState">公共网络防火墙(禁用:0;启用(默认):1)</param>
/// <param name="standardState">专用网络防火墙(禁用:0;启用(默认):1)</param>
/// <returns></returns>
public static bool FirewallOperateByRegistryKey(int domainState=1, int publicState = 1, int standardState = 1)
{
  RegistryKey key = Registry.LocalMachine;
  try
  {
    string path = "HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\SharedAccess\\Defaults\\FirewallPolicy";
    RegistryKey firewall = key.OpenSubKey(path, true);
    RegistryKey domainProfile = firewall.OpenSubKey("DomainProfile", true);
    RegistryKey publicProfile = firewall.OpenSubKey("PublicProfile", true);
    RegistryKey standardProfile = firewall.OpenSubKey("StandardProfile", true);
    domainProfile.SetValue("EnableFirewall", domainState, RegistryValueKind.DWord);
    publicProfile.SetValue("EnableFirewall", publicState, RegistryValueKind.DWord);
    standardProfile.SetValue("EnableFirewall", standardState, RegistryValueKind.DWord);
  }
  catch (Exception e)
  {
    string error = $"注册表修改出错:{e.Message}";
    throw new Exception(error);
  }
  return true;
}
3、直接操作防火墙对象
0 Q) d2 O4 x! f, g; }$ L9 c       需要在项目引用中添加对NetFwTypeLib的引用,并引用命名空间NetFwTypeLib
3 Z! Q2 }$ H% i$ d6 R0 ~0 |9 l  I
/// <summary>
/// 通过对象防火墙操作
/// </summary>
/// <param name="isOpenDomain">域网络防火墙(禁用:false;启用(默认):true)</param>
/// <param name="isOpenPublicState">公共网络防火墙(禁用:false;启用(默认):true)</param>
/// <param name="isOpenStandard">专用网络防火墙(禁用: false;启用(默认):true)</param>
/// <returns></returns>
public static bool FirewallOperateByObject(bool isOpenDomain = true, bool isOpenPublicState = true, bool isOpenStandard = true)
{
  try
  {
    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
    // 启用<高级安全Windows防火墙> - 专有配置文件的防火墙
    firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, isOpenStandard);
    // 启用<高级安全Windows防火墙> - 公用配置文件的防火墙
    firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, isOpenPublicState);
    // 启用<高级安全Windows防火墙> - 域配置文件的防火墙
    firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN, isOpenDomain);
  }
  catch (Exception e)
  {
    string error = $"防火墙修改出错:{e.Message}";
    throw new Exception(error);
  }
  return true;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2025-4-30 17:25

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表