您好,我正在使用此处的 WindowsFirewallHelper Lib:WindowsFirewallHelper Git
首先我创建一条规则
IRule rule = FirewallManager.Instance.CreateApplicationRule(
FirewallManager.Instance.GetProfile().Type,
ruleName,
FirewallAction.Block,
@"Path\App.exe"
);
rule.Direction = FirewallDirection.Outbound;
FirewallManager.Instance.Rules.Add(rule);
之后,我想创建一个连接和断开连接方法来启用或禁用此规则,但我在库中找不到任何方法,有人知道如何做到这一点吗? 只有“rule.isEnabled”字段告诉它是否启用。
最佳答案
由于我无法使用 Lib 实现这一点:我对启用和禁用方法执行了以下操作:
我创建了一个 CMD 方法
private static void RunCMD(string argument)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = @"C:\Windows\System32\cmd.exe";
startInfo.Arguments = argument;
process.StartInfo = startInfo;
process.Start();
}
然后我添加了两种其他方法:
public static void Disconnect()
{
RunCMD(@"/C netsh advfirewall firewall set rule name=""RULENAME"" new enable=no");
}
public static void Connect()
{
RunCMD(@"/C netsh advfirewall firewall set rule name=""RULENAME"" new enable=yes");
}
关于c# - 使用 WindowsFirewallHelper c# 启用和禁用 Windows 防火墙规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65844171/