Use NetworkInformation classes to get the list of listening ports on your machine

.Net frameworks 2.0 have new addition of Namespace, System.Net.NetworkInformation, it provide a number of interesting classes to extract the network related statistics and state of the machine, it pretty much provide most of the functionality which is exposed by native IPHelper APIs.

Earlier I had shown a simple example for getting network availbility event notification. Here is another simple 4 line example, where your application could check all the listening ports on the machine.

using System;
using System.Net;
using System.Net.NetworkInformation;
public class Test
      public static void Main()
            IPGlobalProperties ipGlobal = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] connections = ipGlobal.GetActiveTcpListeners ( ) ;
            foreach(IPEndPoint ipe in connections)
           Console.WriteLine("Listening IPEndPoint = "+ipe.Port);


Comments (5)
  1. Interesting! It would be even better if it provided a way to get the process associated to each end point (as netstat -o does).

  2. Very nice!

    Can you comment or blog on whether we’ll be able to connect via HttpWebRequest through a proxy configured with an automatic configuration script? I cannot write managed code to go beyond our firewall without this.


  3. adarshk says:

    Hi Lionel,

    Thanks for pointing this out, current 2.0 would not allow to get process assicated to the endpoint, we have this feature in our to do list for future version.


  4. adarshk says:

    Hi John,

    Yes Autoproxy support was one of the highly requested feature, it is supported in Whidbey (v2.0) version.



Comments are closed.

Skip to main content