Network Information sample code


using System;


using System.Collections.Generic;


using System.Text;


using System.Net.NetworkInformation;    // NetworkInformation


 


namespace NetworkInfo


{


    class Program


    {


        static int Main(string[] args)


        {


            if (!NetworkInterface.GetIsNetworkAvailable())


                return 1;                // Command line return failure


            Console.SetWindowSize(50, 50);


            // New Network features


            SimplePing("AMachineName");


            ShowInterfaceSummary();


            ShowTcpConnectionStatistics();


            ShowInboundIPStatistics();


            Console.ResetColor();


            Console.SetCursorPosition(0, 0);


            Console.ReadLine();


            return 0;                   // Command line return success


        }   // Main()


 


        public static void SimplePing(string machine)


        {


            Ping pingSender = new Ping();


            PingReply reply = pingSender.Send(machine);


            Console.BackgroundColor = ConsoleColor.Blue;


            Console.WriteLine("Ping: {0}", machine);


            Console.ResetColor();


            if (reply.Status == IPStatus.Success)


            {


                Console.WriteLine("Address: {0}", reply.Address.ToString());


                Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);


                Console.WriteLine("Time to live: {0}", reply.Options.Ttl);


                Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);


                Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);


            }   // if


            else


            {


                Console.WriteLine(reply.Status);


            }   // else


            Console.WriteLine();


        }   // SimplePing


 


        public static void ShowInterfaceSummary()


        {


            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();


            Console.BackgroundColor = ConsoleColor.Blue;


            Console.WriteLine("NetworkInterfaces");


            Console.ResetColor();


            foreach (NetworkInterface adapter in interfaces)


            {


                Console.WriteLine("Name: {0}", adapter.Name);


                Console.WriteLine(adapter.Description);


                Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));


                Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);


                Console.WriteLine("  Operational status ...................... : {0}",


                    adapter.OperationalStatus);


                string versions = "";


 


                // Create a display string for the supported IP versions.


                if (adapter.Supports(NetworkInterfaceComponent.IPv4))


                {


                    versions = "IPv4";


                }


                if (adapter.Supports(NetworkInterfaceComponent.IPv6))


                {


                    if (versions.Length > 0)


                    {


                        versions += " ";


                    }


                    versions += "IPv6";


                }


                Console.WriteLine("  IP version .............................. : {0}", versions);


                Console.WriteLine();


            }


            Console.WriteLine();


        }   // ShowInterfaceSummary()


 


        public static void ShowTcpConnectionStatistics()


        {


            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();


            TcpStatistics tcpstat = properties.GetTcpIPv4Statistics();


            Console.BackgroundColor = ConsoleColor.Blue;


            Console.WriteLine("  Connection Data:");


            Console.ResetColor();


            Console.WriteLine("      Current  ............................ : {0}",


                tcpstat.CurrentConnections);


            Console.WriteLine("      Cumulative .......................... : {0}",


                tcpstat.CumulativeConnections);


            Console.WriteLine("      Initiated ........................... : {0}",


                tcpstat.ConnectionsInitiated);


            Console.WriteLine("      Accepted ............................ : {0}",


                tcpstat.ConnectionsAccepted);


            Console.WriteLine("      Failed Attempts ..................... : {0}",


                tcpstat.FailedConnectionAttempts);


            Console.WriteLine("      Reset ............................... : {0}",


                tcpstat.ResetConnections);


            Console.WriteLine("      Errors .............................. : {0}",


                tcpstat.ErrorsReceived);


            Console.WriteLine();


        }   // ShowTcpConnectionStatistics()


 


 


        public static void ShowInboundIPStatistics()


        {


            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();


            IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();


            Console.BackgroundColor = ConsoleColor.Blue;


            Console.WriteLine("  Inbound Packet Data:");


            Console.ResetColor();


            Console.WriteLine("      Received ............................ : {0}",


            ipstat.ReceivedPackets);


            Console.WriteLine("      Forwarded ........................... : {0}",


            ipstat.ReceivedPacketsForwarded);


            Console.WriteLine("      Delivered ........................... : {0}",


            ipstat.ReceivedPacketsDelivered);


            Console.WriteLine("      Discarded ........................... : {0}",


            ipstat.ReceivedPacketsDiscarded);


            Console.WriteLine();


        }   // ShowInboundIPStatistics


 


    }   // class Program


}   // namespace NetworkInfo


 

Comments (2)
  1. This was my first webcast, so please be patient through the beginning.  It gets much better there…

Comments are closed.

Skip to main content