当前位置:网站首页>A brief introduction of C code to open or close the firewall example
A brief introduction of C code to open or close the firewall example
2020-11-08 23:48:00 【That's how Linux should learn】
| This article mainly introduces c# Examples of turning firewalls on or off by code , To help people better understand and use c#, Interested friends can learn about |
There are two ways to operate firewalls through code : First, code operation to modify the registry, enable or close the firewall ; The other is to directly operate the firewall object to enable or close the firewall . Either way , You need to use administrator rights , Therefore, it is necessary to determine whether the program has administrator rights before operation .
You need to reference a namespace :System.Security.Principal
/// Determine whether the program has administrator rights
/// true: It's the administrator ;false: Not an administrator
public static bool IsAdministrator()
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
You need to reference a namespace :Microsoft.Win32
/// Through the registry operation firewall
/// Domain network firewall ( Ban :0; Enable ( Default ):1)
/// Public network firewall ( Ban :0; Enable ( Default ):1)
/// Private network firewall ( Ban :0; Enable ( Default ):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 = $" Error modifying registry :{e.Message}";
throw new Exception(error);
}
return true;
}
You need to add a reference to the project NetFwTypeLib References to , And reference the namespace NetFwTypeLib
/// Through the object firewall operation
/// Domain network firewall ( Ban :false; Enable ( Default ):true)
/// Public network firewall ( Ban :false; Enable ( Default ):true)
/// Private network firewall ( Ban : false; Enable ( Default ):true)
public static bool FirewallOperateByObject(bool isOpenDomain = true, bool isOpenPublicState = true, bool isOpenStandard = true)
{
try
{
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
// Enable < Advanced security Windows A firewall > - Proprietary profile firewall
firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, isOpenStandard);
// Enable < Advanced security Windows A firewall > - Firewall for public profiles
firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, isOpenPublicState);
// Enable < Advanced security Windows A firewall > - Domain profile firewall
firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN, isOpenDomain);
}
catch (Exception e)
{
string error = $" Firewall modification error :{e.Message}";
throw new Exception(error);
}
return true;
}
That's all c# Turn on or off the details of the firewall by code .
This paper addresses :https://www.linuxprobe.com/close-linux-open.html
版权声明
本文为[That's how Linux should learn]所创,转载请带上原文链接,感谢
边栏推荐
- Decorator (2)
- What are the basic requirements for big data posts?
- Travel notes of csp-s 2020
- Combine theory with practice to understand CORS thoroughly
- Table join
- salesforce零基础学习(九十八)Salesforce Connect & External Object
- LeetCode-11:盛水最多的容器
- 接口测试工具Eolinker进行post请求
- Five factors to consider before choosing API management platform
- android开发中提示:requires permission android.permission write_settings解决方法
猜你喜欢

Salesforce connect & external object

Copy on write collection -- copyonwritearraylist

Concurrent linked queue: a non blocking unbounded thread safe queue

Programmers should know the URI, a comprehensive understanding of the article

你有没有想过为什么交易和退款要拆开不同的表

Execution of SQL statement

AQS 都看完了,Condition 原理可不能少!

App crashed inexplicably. At first, it thought it was the case of the name in the header. Finally, it was found that it was the fault of the container!

基于链表的有界阻塞队列 —— LinkedBlockingQueue

使用递增计数器的线程同步工具 —— 信号量,它的原理是什么样子的?
随机推荐
Copy on write collection -- copyonwritearraylist
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
Experiment 1 assignment
Mobile big data own website precise marketing and accurate customer acquisition
APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!
Computer network application layer
Combine theory with practice to understand CORS thoroughly
JVM真香系列:轻松理解class文件到虚拟机(下)
Octave basic syntax
Chapter five
c++11-17 模板核心知识(二)—— 类模板
如何将 PyTorch Lightning 模型部署到生产中
SQL语句的执行
centos7下安装iperf时出现 make: *** No targets specified and no makefile found. Stop.的解决方案
小议缓冲区溢出
CSP-S 2020 游记
STS安装
华为HCIA笔记
老大问我:“建表为啥还设置个自增 id ?用流水号当主键不正好么?”
几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!