Posts Tagged ‘IPGLOBALPROPERTIES’
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();
}
}
}
Getting TCP/IP Statistic through C#
Getting the TCP/IP Statistic is very easy through C#. Since all the required methods and properties are there in framework. To get the TCP/IP Statistic we have to use the following nampespace
using System.Net.NetworkInformation;
Now to get the statistic we have to IPGlobalProperties class and extract the information out of it to our TCP/IP statistic object
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpStatistics tcpstat = null;
now with following line you will get all the details for IPv4
tcpstat = properties.GetTcpIPv4Statistics();
and to get the details of IPv6 you use following line
tcpstat = properties.GetTcpIPv6Statistics();
now we can get details from this object. Details like data sent, recieved, total connections and various other