I need to add a firewall rule with my WPF program. In Powershell i have a working solution:
New-NetFirewallRule -DisplayName $Description -Direction Inbound -Action Allow -Profile Any -EdgeTraversalPolicy Allow -Protocol TCP -LocalPort 80, 443.
Is quite simple.
What is the equivalent in C#? netsh? I found this https://stackoverflow.com/a/29652140/10194386:
System.Diagnostics.Process.Start("netsh.exe", "whatever you would need to write as parameters");
Is there a better solution?
Thanks a lot!
Edit:
I tryed the link from @Cpt.Whale stackoverflow.com/a/34018032/7411885
private void btn_set_FW_Click(object sender, RoutedEventArgs e)
        {
            Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
            INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
            var currentProfiles = fwPolicy2.CurrentProfileTypes;
            // Let's create a new rule
            INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
            inboundRule.Enabled = true;
            //Allow through firewall
            inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
            //Using protocol TCP
            inboundRule.Protocol = 6; // TCP
                                      
            inboundRule.LocalPorts = "81"; //Port 81
            //Name of rule
            inboundRule.Name = "MyRule";
            // ...//
            inboundRule.Profiles = currentProfiles;
            // Now add the rule
            INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            firewallPolicy.Rules.Add(inboundRule);
        }
But i get some errors:
CS1061 - "INetFwRule2" does not contain a definition for "Protocol". CS1061 - "INetFwRule2" does not contain a definition for "LocalPorts". CS1061 - "INetFwRule2" does not contain a definition for "Name". CS1061 - "INetFwRule2" does not contain a definition for "Profiles". CS5103 - Argument "1": Conversion of "MyAPP.INetFwRule2" to "NetFwTypeLib.INetFwRule" not possible.
where is my mistake?