当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

C/C++编程笔记:指针篇!从内存理解指针,让你完全搞懂指针

程序员都应该知道的URI,一文帮你全面了解

Copy on write collection -- copyonwritearraylist

Fiddler can't grab requests from browsers like Google_ Solution

实现图片的复制

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

表连接

STS安装

14.Kubenetes简介

C / C + + Programming Notes: pointer! Understand pointer from memory, let you understand pointer completely
随机推荐
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!
Copy the picture
STS安装
Save code
Mycat搭建
移动大数据自有网站精准营销精准获客
The vowels in the inverted string of leetcode
Linked list
How to reduce the resource consumption of istio agent through sidecar custom resource
Execution of SQL statement
链表
API生命周期的5个阶段
Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)
装饰器(二)
Programmers should know the URI, a comprehensive understanding of the article
Concurrent linked queue: a non blocking unbounded thread safe queue
Realization of file copy
LeetCode-15:三数之和
实现图片的复制
Computer network application layer