PromatWindowsFirewallManagement is available by NuGet PromatWindowsFirewallManagement
The library is implemented in netstandard 2, so it can be used in all types of projects:
Both in core and full framework we have to instantiate the WindowsFirewallRule class,
on it we can define the configuration of the rule.
In the configuration we have to take into account that the Name, Dir and Action parameters are mandatory
Once configured we can use its AddRule method to add the rule or DeleteRule to delete it.
Configuration:
var rule = new WindowsFirewallRule
{
Name = "Microsoft SQL Server 2014 TCP Port 1433",
Dir = DirEnum.In,
Action = ActionEnum.Allow,
Protocol = ProtocolEnum.Tcp,
Profile = ProfileEnum.Private | ProfileEnum.Domain
};
rule.AddLocalPorts(1433);
Add / Delete:
rule.AddRule();
rule.DeleteRule();