时间:2021-05-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;}以上就是c# 通过代码开启或关闭防火墙的详细内容,更多关于c# 防火墙的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1.查看防火墙状态查看防火墙状态systemctlstatusfirewalld开启防火墙systemctlstartfirewalld关闭防火墙systemc
phpstudylinux(小皮面板)防火墙的开启和关闭方法非常简单,点击左侧面板【服务器防火墙】,选择左侧防火墙【开启】/【关闭】按钮,进行防火墙设置即可。具
xp防火墙在哪里设置xp系统如何关闭防火墙xp防火墙在哪里设置xp系统如何关闭防火墙二、关闭防火墙方法二 关闭防火墙进程或者直接在右下角防火墙图标点关闭,如果
sc防火墙使用的方法: 1、鼠标右点击:网络,属性。 2、点击:Windows防火墙。3、点击:启用或关闭Windows防火墙 4、点击:启用Window
(二)防火墙配置查看防火墙状态:/etc/init.d/iptablesstatus暂时关闭防火墙:/etc/init.d/iptablesstop禁止防火墙在