簡單介紹c#透過程式碼開啟或關閉防火牆示例
這篇文章主要介紹了c# 透過程式碼開啟或關閉防火牆的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以瞭解下 |
透過程式碼操作防火牆的方式有兩種:一是程式碼操作修改登錄檔啟用或關閉防火牆;二是直接操作防火牆物件來啟用或關閉防火牆。不論哪一種方式,都需要使用管理員許可權,所以操作前需要判斷程式是否具有管理員許可權。
1、判斷程式是否擁有管理員許可權
需要引用名稱空間:System.Security.Principal
/// 判斷程式是否擁有管理員許可權 ///true:是管理員;false:不是管理員public static bool IsAdministrator() { WindowsIdentity current = WindowsIdentity.GetCurrent(); WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current); return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); }
2、登錄檔修改防火牆
需要引用名稱空間: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; }
3、直接操作防火牆物件
需要在專案引用中新增對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# 透過程式碼開啟或關閉防火牆的詳細內容。
原文地址:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31559985/viewspace-2732841/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何開啟或關閉win7自帶防火牆?Win7防火牆
- ubuntu 開啟/關閉ubuntu防火牆Ubuntu防火牆
- linux關閉防火牆命令 linux防火牆關閉和開啟命令Linux防火牆
- 教你如何開啟/關閉ubuntu防火牆Ubuntu防火牆
- 開啟、關閉防火牆或者開放埠防火牆
- CentOS8檢視防火牆狀態,開啟/關閉防火牆CentOS防火牆
- Linux下關閉和開啟防火牆Linux防火牆
- Linux防火牆的關閉和開啟Linux防火牆
- win10防火牆關閉還是開啟好_win10防火牆關閉開啟有什麼影響Win10防火牆
- ubuntu下開啟/關閉防火牆 及埠 - 命令Ubuntu防火牆
- Linux防火牆的關閉、開啟和配置Linux防火牆
- 防火牆介紹防火牆
- ubuntu 關閉防火牆命令 ubuntu怎樣關閉防火牆Ubuntu防火牆
- CentOS關閉防火牆CentOS防火牆
- OpenSUSE關閉防火牆防火牆
- [譯]WebAssembly: 帶有程式碼示例的簡單介紹Web
- CentOS7使用firewalld開啟關閉防火牆與埠CentOS防火牆
- Linux下檢視、關閉及開啟防火牆命令Linux防火牆
- win10關閉防火牆方法_w10如何關閉防火牆Win10防火牆
- centos7關閉防火牆命令 centos7永久防火牆關閉CentOS防火牆
- win10如何關閉啟用防火牆通知_win10關閉啟動防火牆通知的設定方法Win10防火牆
- CentOS 7關閉防火牆CentOS防火牆
- 關閉centos的防火牆CentOS防火牆
- linux 關閉防火牆Linux防火牆
- 防火牆介紹(1)(轉)防火牆
- 防火牆介紹(2)(轉)防火牆
- centos7怎麼檢視、開啟和關閉防火牆CentOS防火牆
- win10怎麼關閉防火牆 win10關閉防火牆的方法Win10防火牆
- 透過示例程式碼介紹Docker部署Mysql叢集的實現DockerMySql
- 關於各類防火牆的介紹(2)(轉)防火牆
- 關於各類防火牆的介紹(1)(轉)防火牆
- Linux中關閉防火牆Linux防火牆
- CentOS7關閉防火牆CentOS防火牆
- Centos7 關閉防火牆CentOS防火牆
- Juniper防火牆簡介(轉)防火牆
- canvas繪製圓形圖案程式碼示例簡單介紹Canvas
- linux關閉防火牆命令是什麼 linux永久關閉防火牆命令分享Linux防火牆
- 簡單介紹JavaScript閉包JavaScript