共用方式為


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

  • Anonymous
    March 14, 2005
    Interesting! It would be even better if it provided a way to get the process associated to each end point (as netstat -o does).
  • Anonymous
    March 22, 2005
    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.

    John
  • Anonymous
    March 22, 2005
    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.
    Thanks
  • Anonymous
    March 22, 2005
    Hi John,
    Yes Autoproxy support was one of the highly requested feature, it is supported in Whidbey (v2.0) version.

    Thanks
    adarsh
  • Anonymous
    November 25, 2007
    PingBack from http://feeds.maxblog.eu/item_106773.html