```javascript
/// <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 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
try
{
string path = @"SYSTEM\ControlSet001\Services\SharedAccess\Parameters\FirewallPolicy";//这里目前针对win10 64位有效
RegistryKey firewall = key.OpenSubKey(path, true);
if (firewall == null)
{
File.AppendAllText(Environment.CurrentDirectory+"\\filewall.log", "打开路径:" + path + "返回空值" + Environment.NewLine);
return false;
}
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}";
File.AppendAllText(Environment.CurrentDirectory + "\\filewall.log", error + Environment.NewLine);
throw new Exception(error);
}
return true;
}
```