欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 通過代碼開啟或關(guān)閉防火墻

 更新時間:2020年10月28日 15:46:19   作者:一只獨行的猿  
這篇文章主要介紹了c# 通過代碼開啟或關(guān)閉防火墻的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

  通過代碼操作防火墻的方式有兩種:一是代碼操作修改注冊表啟用或關(guān)閉防火墻;二是直接操作防火墻對象來啟用或關(guān)閉防火墻。不論哪一種方式,都需要使用管理員權(quán)限,所以操作前需要判斷程序是否具有管理員權(quán)限。

  1、判斷程序是否擁有管理員權(quán)限

  需要引用命名空間:System.Security.Principal

/// <summary>
/// 判斷程序是否擁有管理員權(quán)限
/// </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">域網(wǎng)絡(luò)防火墻(禁用:0;啟用(默認(rèn)):1)</param>
/// <param name="publicState">公共網(wǎng)絡(luò)防火墻(禁用:0;啟用(默認(rèn)):1)</param>
/// <param name="standardState">專用網(wǎng)絡(luò)防火墻(禁用:0;啟用(默認(rèn)):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">域網(wǎng)絡(luò)防火墻(禁用:false;啟用(默認(rèn)):true)</param>
/// <param name="isOpenPublicState">公共網(wǎng)絡(luò)防火墻(禁用:false;啟用(默認(rèn)):true)</param>
/// <param name="isOpenStandard">專用網(wǎng)絡(luò)防火墻(禁用: false;啟用(默認(rèn)):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# 通過代碼開啟或關(guān)閉防火墻的詳細(xì)內(nèi)容,更多關(guān)于c# 防火墻的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論