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\hnetcfg.dll)
using System;
using NetFwTypeLib;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
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.GetTypeFromProgID("HNetCfg.FwMgr");
icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
return;
}
try
{
INetFwProfile profile;
INetFwOpenPort portClass;
Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");
portClass = (INetFwOpenPort)Activator.CreateInstance(TportClas s);
// Get the current profile
profile = icfMgr.LocalPolicy.CurrentProfile;
// Set the port properties
portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
portClass.Enabled = true;
portClass.Name = "TestPort";
portClass.Port = 1124;
portClass.Protocol =
NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCO L_TCP;;
// Add the port to the ICF Permissions List
profile.GloballyOpenPorts.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.