当前位置:网站首页>简单介绍c#通过代码开启或关闭防火墙示例
简单介绍c#通过代码开启或关闭防火墙示例
2020-11-08 23:48:00 【Linux就该这么学】
| 这篇文章主要介绍了c# 通过代码开启或关闭防火墙的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下 |
通过代码操作防火墙的方式有两种:一是代码操作修改注册表启用或关闭防火墙;二是直接操作防火墙对象来启用或关闭防火墙。不论哪一种方式,都需要使用管理员权限,所以操作前需要判断程序是否具有管理员权限。
需要引用命名空间:System.Security.Principal
/// 判断程序是否拥有管理员权限
/// true:是管理员;false:不是管理员
public static bool IsAdministrator()
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
需要引用命名空间:Microsoft.Win32
/// 通过注册表操作防火墙
/// 域网络防火墙(禁用:0;启用(默认):1)
/// 公共网络防火墙(禁用:0;启用(默认):1)
/// 专用网络防火墙(禁用:0;启用(默认):1)
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;
}
需要在项目引用中添加对NetFwTypeLib的引用,并引用命名空间NetFwTypeLib
/// 通过对象防火墙操作
/// 域网络防火墙(禁用:false;启用(默认):true)
/// 公共网络防火墙(禁用:false;启用(默认):true)
/// 专用网络防火墙(禁用: false;启用(默认):true)
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# 通过代码开启或关闭防火墙的详细内容。
版权声明
本文为[Linux就该这么学]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3585265/blog/4708413
边栏推荐
- Using containers to store table data
- Realization of file copy
- JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)
- 给大家介绍下,这是我的流程图软件 —— draw.io
- LeetCode-15:三数之和
- salesforce零基础学习(九十八)Salesforce Connect & External Object
- Iterm2 configuration and beautification
- . net core cross platform resource monitoring library and dotnet tool
- Web上的分享(Share)API
- 动态规划设计:最大子数组
猜你喜欢

Creating a text cloud or label cloud in Python

Experiment 1 assignment

Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection

老大问我:“建表为啥还设置个自增 id ?用流水号当主键不正好么?”

Mycat搭建

为什么需要使用API管理平台

Using fastai to develop and deploy image classifier application

200 programmers interview experience, all here

Dynamic query processing method of stored procedure

如何让脚本同时兼容Python2和Python3?
随机推荐
Problem solving templates for subsequence problems in dynamic programming
Dynamic planning
给大家介绍下,这是我的流程图软件 —— draw.io
Factory Pattern模式(简单工厂、工厂方法、抽象工厂模式)
【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明
如何将 PyTorch Lightning 模型部署到生产中
Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
使用容器存储表格数据
理论与实践相结合彻底理解CORS
200 programmers interview experience, all here
C/C++学习日记:原码、反码和补码
大数据岗位基础要求有哪些?
解决go get下载包失败问题
STC转STM32第一次开发
非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue
你有没有想过为什么交易和退款要拆开不同的表
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
VIM Introduction Manual, (vs Code)
How to analyze Android anr problems
数组初相识