Create window Inbound firewall rule for sql server 1433 port through programmatically

Create window Inbound firewall rule for sql server 1433 port through programmatically

Here you can create inbound firewall rule for sql server 1433 port with multiprofile Domain, Public and Private through C#.

First you have to import a dll from your system “C:\Windows\System32/FirewallAPI.dll”.
Add this DLL to your project reference.
Go to solution explorer in visual studio -> References->Add Reference->click on Browse button near ok and cancel button -> browse path C:\Windows\System32/FirewallAPI.dll and add the dll.

After adding dll use namespace “NetFwTypeLib” in your code like this at above program.
using NetFwTypeLib;


using NetFwTypeLib;

namespace ConsoleAppTestDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
            INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);

            // 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;

            //For all profile
            inboundRule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;

            //Using protocol TCP
            inboundRule.Protocol = 6; // TCP
            //Local Port 1433
            inboundRule.LocalPorts = "1433";
            //Name of rule
            inboundRule.Name = "SQLRule";
           
            // Now add the rule
            INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            firewallPolicy.Rules.Add(inboundRule);
        }
    }
}

After that you can check your firewall inbound rule.