Reputation: 1586
I would like to add exception to windows firewall for sql server express 2008 R2 using C#.
How can I do this?
Scenario:
How can this be done using C#, I would like to do it automatically on the first application start or in the setup process with custom actions.
Upvotes: 1
Views: 3344
Reputation: 3802
Try this link. It wraps up access to the windows firewall API.
Should do the trick.
Actually this looks better --> Automating Windows Firewall Settings with C#
You should be able to open up the port you want using this
1: INetFwOpenPorts ports;
2: INetFwOpenPort port;
3: port.Port = 1433; /* port no */
4: port.Name = “Application1”; /*name of the application using the port */
5: port.Enabled = true; /* enable the port */
6: /*other properties like Protocol, IP Version can also be set accordingly
7: now add this to the GloballyOpenPorts collection */
8:
9: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
10: INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
11: ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
12:
13: ports.Add(port);
14:
Note in Visual Studio, you need to add NetFwTypeLib COM
reference to your project and also include NetFwTypeLib in your project ( using NetFwTypeLib;
)
Upvotes: 1
Reputation: 376
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.
Upvotes: 0
Reputation: 74899
Here are a few options:
Windows XP SP2 Firewall Controller
http://www.codeproject.com/KB/winsdk/WinXPSP2Firewall.aspx
Controlling Windows Firewall using C# via COM Interop
http://www.shafqatahmed.com/2008/01/controlling-win.html
Netsh Command Syntax for the Netsh Firewall Context
http://technet.microsoft.com/en-us/library/bb490617.aspx
Upvotes: 1
Reputation: 3084
It is possible to do this quite easily in C# among other neat things with windows firewall, see this article for more information Controlling Windows Firewall C#
You can also take a look at the answer posted here.
Upvotes: 2