c# 通过代码开启或关闭防火墙
作者:一只独行的猿 时间:2021-12-11 19:53:20
通过代码操作防火墙的方式有两种:一是代码操作修改注册表启用或关闭防火墙;二是直接操作防火墙对象来启用或关闭防火墙。不论哪一种方式,都需要使用管理员权限,所以操作前需要判断程序是否具有管理员权限。
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;
}
来源:https://www.cnblogs.com/pilgrim/p/11135485.html
标签:c#,防火墙
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java接口中尽量避免使用数组
2022-07-06 23:24:14
![](https://img.aspxhome.com/file/2023/0/69930_0s.png)
MyBatis控制台显示SQL语句的方法实现
2021-10-18 04:10:00
![](https://img.aspxhome.com/file/2023/4/99864_0s.png)
详解java IO流之缓冲流的使用
2023-08-08 18:33:16
C# WinForm调用Shell_NotifyIcon的示例代码
2021-07-17 16:04:41
java之Object类用法实例
2023-11-05 04:14:26
![](https://img.aspxhome.com/file/2023/8/58828_0s.png)
Springboot 在普通类型注入Service或mapper
2023-11-29 15:26:21
C# SynchronizationContext以及Send和Post使用解读
2023-10-16 04:27:28
Android WindowManger实现桌面悬浮窗功能
2023-08-01 02:16:10
![](https://img.aspxhome.com/file/2023/6/84706_0s.webp)
Java Apache Shiro安全框架快速开发详解流程
2022-06-21 23:00:23
![](https://img.aspxhome.com/file/2023/8/69648_0s.png)
为什么wait和notify必须放在synchronized中使用
2022-07-20 07:32:39
![](https://img.aspxhome.com/file/2023/7/71187_0s.webp)
java实现省市区转换成树形结构
2022-11-27 03:43:37
![](https://img.aspxhome.com/file/2023/4/84264_0s.jpg)
c#图片处理之图片裁剪成不规则图形
2023-02-23 02:43:58
![](https://img.aspxhome.com/file/2023/1/86981_0s.jpg)
java实现简单斗地主(看牌排序)
2023-09-12 14:42:40
Android实现带指示器的自动轮播式ViewPager
2022-07-25 22:28:49
![](https://img.aspxhome.com/file/2023/2/97852_0s.gif)
C#实现回文检测的方法
2022-12-26 22:31:23
手把手教你设置IntelliJ IDEA 的彩色代码主题的图文教程
2022-08-22 00:15:25
![](https://img.aspxhome.com/file/2023/0/74980_0s.jpg)
Mybatis-plus基于redis实现二级缓存过程解析
2021-12-08 23:34:43
![](https://img.aspxhome.com/file/2023/1/63151_0s.png)
Java 回调callback举例详解
2023-11-11 16:25:09
![](https://img.aspxhome.com/file/2023/2/59592_0s.png)
Android线程间通信 Handler使用详解
2022-10-16 09:22:33
解决JavaWeb读取本地json文件以及乱码的问题
2023-09-14 18:35:14