c# - 使用 WindowsFirewallHelper c# 启用和禁用 Windows 防火墙规则

标签 c# windows-firewall

您好,我正在使用此处的 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/

相关文章:

c# - 部署 NetFwTypeLib 以管理 Windows 防火墙

c# - 如何在 Azure 通信服务聊天中附加文件?

windows - windows下TCP包拦截与重定向

某些分发命令的 python SSL 证书验证失败

c# - C# 中的字节数组加密

azure - 从本地 Windows 10 盒子连接到 azure 服务总线失败

powershell - 如何找到尝试连接的端口?

c# - 如何更改 Ninject 中现有绑定(bind)的范围

c# - 为什么 Entity Framework 会在直接sql立即返回时计算语句超时?

c# - 什么是 R60、R72 和 MINE 文件?