1 Replies - 2208 Views - Last Post: 08 May 2013 - 05:39 AM Rate Topic: -----

#1 seta   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 11

Obtain the list of open ports, windows firewall

Posted 08 May 2013 - 02:09 AM

Guys i am trying to develop a code that manages the firewall ports using vb.net. The first part is to list all ports enabled. so I am trying this code:

    Function portsList()
        Dim ports As INetFwOpenPorts
        Dim port As INetFwOpenPort
        Dim myPorts() As INetFwOpenPorts

        Dim NetFwMgrType As Type = Type.GetTypeFromProgID("HNetCfg.FwMgr", False)
        Dim mgr As INetFwMgr = DirectCast(Activator.CreateInstance(NetFwMgrType), INetFwMgr)
        ports = DirectCast(mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts, INetFwOpenPorts)
        Dim enumerate As System.Collections.IEnumerator = ports.GetEnumerator()

        Dim i As Integer
        While enumerate.MoveNext()
            port = DirectCast(enumerate.Current, INetFwOpenPort)
            myPorts(i) = port
            i += 1
        End While

        Dim portAsString() As String
        For j As Integer = 0 To i
            portAsString(j) = myPorts(j).ToString

        Return portAsString
    End Function

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim ports() As String = portsList()
        Dim n As String = ports.Length
        Dim newString As String = ""

        For h As Integer = 0 To n
            newString = ports(h) & vbNewLine

        RichTextBox1.Text = newString
    End Sub

What i want to do is list all the ports in Richtextbox1 after clicking Button4. The error that I am getting is NullReferenceException was unHandled. Object reference not set to an instance of an object. I am new to Vb, hw can i get over this.

Is This A Good Question/Topic? 0
  • +

Replies To: Obtain the list of open ports, windows firewall

#2 modi123_1   User is offline

  • Suitor #2
  • member icon

Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Obtain the list of open ports, windows firewall

Posted 08 May 2013 - 05:39 AM

Which line is it happening on?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1