简体   繁体   中英

Enable and Disable Windows Firewall Rule with WindowsFirewallHelper c#

Hi im using the WindowsFirewallHelper Lib from here: WindowsFirewallHelper Git

First I Create a Rule

 IRule rule = FirewallManager.Instance.CreateApplicationRule(
            FirewallManager.Instance.GetProfile().Type,
            ruleName,
            FirewallAction.Block,
            @"Path\App.exe"
            );
            rule.Direction = FirewallDirection.Outbound;
            FirewallManager.Instance.Rules.Add(rule);

After that I would like to make a Connect and Disconect Method which enables or disables this rule, but I cant find any Method for it within the lib, does anybody know how to do that? There is only the "rule.isEnabled" field which tells if it is enabled or not.

Since I couldnt make it happen with the Lib: I did the following for enable and disable methods:

I created a CMD Method

       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();
    }

Then I added both Other Methods:

   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");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM