Netstat in C#

Check out how to perform Netstat command functions of DOS in C#. It will list you all the current network connections of your System


using System;
using System.Net;
using System.Net.NetworkInformation;

namespace MyNetstat
{
class Program    
{
static void Main(string[] args)
{
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners();
TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();

foreach (TcpConnectionInformation info in tcpConnections)
{
Console.WriteLine("Local : " + info.LocalEndPoint.Address.ToString() 
+ ":" + info.LocalEndPoint.Port.ToString()
+ "\nRemote : " + info.RemoteEndPoint.Address.ToString() 
+ ":" + info.RemoteEndPoint.Port.ToString()
+ "\nState : " + info.State.ToString() + "\n\n");
}
Console.ReadLine();
}
}
}
About these ads
3 comments
  1. sacha bess said:

    Great source ! But this script doesn’t work with Netwok Teaming.

    • A.Sethi said:

      Thanks for update. I will check on that and will try to create solution for that

  2. Eitam Doodai said:

    Hi,

    Great info, but I am missing a way on how to get the Process ID for each connection to know which app is using which connection.

    Eitam.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: