If you are building a Networking Application like i do. you will encounter how to add this things automatically, like adding application to firewall exception, and open the port on firewall,
get the firewall open port, etc.
First you need to add a reference to the Com : NetFwTypeLib.
Read here how to
Note that this person code, is not initiating the applications and ports Object , you will get message null/object reference not set on runtime.
here is how to initiate it and the complete code.
static void AddThisApplicationToFirewallException()
{
Type TappClass= Type.GetTypeFromProgID("HNetCfg.FWAuthorizedApplication");
INetFwAuthorizedApplication application = (INetFwAuthorizedApplication)Activator.CreateInstance(TappClass);
if (!IsApplicationAddedAsExceptionToFirewall())
{
Console.WriteLine("Trying to add This Application Exception To FireWall");
Logger.Write("Trying to add This Application Exception To FireWall", "ClientCategory");
INetFwAuthorizedApplications applications;
application.Name = "Tradix Client Console"; /*set the name of the application */
string fullPath = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().Location);
Console.WriteLine("Full Path of application :" +fullPath);
Logger.Write("Full Path of application :" + fullPath, "ClientCategory");
application.ProcessImageFileName = fullPath;
/* set this property to the location of the executable file of the application*/
application.Enabled = true; //enable it
/*now add this application to AuthorizedApplications collection */
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr) Activator.CreateInstance(NetFwMgrType);
applications = (INetFwAuthorizedApplications) mgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
applications.Add(application);
Console.WriteLine("Application :" + fullPath + " Added To FireWall Exception");
Logger.Write("Application :" + fullPath + " Added To FireWall Exception", "ClientCategory");
Console.ReadKey();
}
}
static bool IsApplicationAddedAsExceptionToFirewall()
{
INetFwAuthorizedApplications applications;
INetFwAuthorizedApplication application;
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
applications = (INetFwAuthorizedApplications)mgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
var enumerate = applications.GetEnumerator();
while (enumerate.MoveNext())
{
application = (INetFwAuthorizedApplication)enumerate.Current;
if(application.Name.Equals("Your application Name"))
{
return true;
}
}
return false;
}
private static List<INetFwOpenPort> openPortsByFireWall=new List<INetFwOpenPort>();
private static void GetOpenPortByFireWall()
{
INetFwOpenPorts ports;
INetFwOpenPort port;
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
//openPortsByFireWall.AddRange((IEnumerable<INetFwOpenPort>) ports);
System.Collections.IEnumerator enumerate = ports.GetEnumerator();
while (enumerate.MoveNext())
{
port = (INetFwOpenPort)enumerate.Current;
openPortsByFireWall.Add(port);
}
}
static void CheckIfPortIsAddToFirewallOrNot(int listeningPort)
{
Logger.Write("Checking listening Port is on the Firewall or not", "ClientCategory");
Console.WriteLine("Checking listening Port is on the Firewall or not");
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
INetFwOpenPorts ports=(INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");
INetFwOpenPort port =(INetFwOpenPort)Activator.CreateInstance(TportClass);
port.Port = listeningPort; /* port no */
port.Name = "Your Application Name"; /*name of the application using the port */
port.Enabled = true; /* enable the port */
/*other properties like Protocol, IP Version can also be set accordingly
now add this to the GloballyOpenPorts collection */
if (openPortsByFireWall.Where(c => c.Port.Equals(listeningPort)).FirstOrDefault() == null)
{
Console.WriteLine("Trying to add Port");
Logger.Write("Trying to add Port", "ClientCategory");
ports.Add(port);
Console.WriteLine("Added Port");
Logger.Write("Added Port", "ClientCategory");
}
}