Hi Martin,
We can use the hnetcfg.dll Com object.
Here is a link you may take a look.
Adding a Port to Current Profile (VBScript)
http://msdn.microsoft.com/library/de...us/ics/ics/wf_
adding_port_to_ profile.asp
We can convert it into C# as below.
Here is the code below.
[NOTE: we need add a COM reference to the hnetcfg.dll
first(<windows> \system32\hnetc fg.dll)
using System;
using NetFwTypeLib;
using System.Runtime. InteropServices ;
using System.Reflecti on;
using System.Diagnost ics;
namespace OpenPort
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
INetFwMgr icfMgr =null;
try
{
Type TicfMgr = Type.GetTypeFro mProgID("HNetCf g.FwMgr");
icfMgr = (INetFwMgr)Acti vator.CreateIns tance(TicfMgr);
}
catch (Exception ex)
{
Trace.WriteLine (ex.ToString()) ;
return;
}
try
{
INetFwProfile profile;
INetFwOpenPort portClass;
Type TportClass = Type.GetTypeFro mProgID("HNetCf g.FWOpenPort");
portClass = (INetFwOpenPort )Activator.Crea teInstance(Tpor tClass);
// Get the current profile
profile = icfMgr.LocalPol icy.CurrentProf ile;
// Set the port properties
portClass.Scope = NetFwTypeLib.NE T_FW_SCOPE_.NET _FW_SCOPE_ALL;
portClass.Enabl ed = true;
portClass.Name = "TestPort";
portClass.Port = 1124;
portClass.Proto col =
NetFwTypeLib.NE T_FW_IP_PROTOCO L_.NET_FW_IP_PR OTOCOL_TCP;;
// Add the port to the ICF Permissions List
profile.Globall yOpenPorts.Add( portClass);
return;
}
catch (Exception ex)
{
Trace.WriteLine (ex.ToString()) ;
}
}
}
}
Best regards,
Perter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.