使用c# 实现开启或关闭防火墙

发布时间:2020-10-29 15:02:43 作者:Leah
来源:亿速云 阅读:413

使用c# 实现开启或关闭防火墙?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

  1、判断程序是否拥有管理员权限

  需要引用命名空间:System.Security.Principal

/// <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、注册表修改防火墙

  需要引用命名空间:Microsoft.Win32

/// <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、直接操作防火墙对象

  需要在项目引用中添加对NetFwTypeLib的引用,并引用命名空间NetFwTypeLib

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

关于使用c# 实现开启或关闭防火墙问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. ubuntu防火墙UFW的安装与使用
  2. 使用GNS3来实现交换机的互联互通

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

防火墙

上一篇:mysql实现横向转纵向与纵向转横向排列的方法

下一篇:PHP中为什么使用xlswriter

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》