NetFwTypeLib RRS feed

All replies

  • Hi,

    add port:

    Imports NetFwTypeLib
    Public Class Form1
        Public Shared Sub NetFwAddPorts(ByVal name As String, ByVal port As Integer, ByVal protocol As String)
            Dim netFwMgr As INetFwMgr = CType(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")), INetFwMgr)
            Dim objPort As INetFwOpenPort = CType(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort")), INetFwOpenPort)
            objPort.Name = name
            objPort.Port = port
            If protocol.ToUpper() = "TCP" Then
                objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP
                objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP
            End If
            objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL
            objPort.Enabled = True
            Dim exist As Boolean = False
            For Each mPort As INetFwOpenPort In netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts
                If objPort Is mPort Then
                    exist = True
                    Exit For
                End If
            If Not exist Then netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort)
        End Sub
    End Class

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, July 22, 2019 5:10 AM