Obtain Information About the Local Network Interface - CSharp Network

CSharp examples for Network:Network Interface

Description

Obtain Information About the Local Network Interface

Demo Code


using System;//from w w w. ja v  a2s  .  co m
using System.Net.NetworkInformation;

class MainClass
    {
        static void Main()
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface ni in interfaces)
                {
                    Console.WriteLine("Interface Name: {0}", ni.Name);
                    Console.WriteLine("    Description: {0}", ni.Description);
                    Console.WriteLine("    ID: {0}", ni.Id);
                    Console.WriteLine("    Type: {0}", ni.NetworkInterfaceType);
                    Console.WriteLine("    Speed: {0}", ni.Speed);
                    Console.WriteLine("    Status: {0}", ni.OperationalStatus);

                    Console.WriteLine("    Physical Address: {0}", ni.GetPhysicalAddress().ToString());

                    Console.WriteLine("    Bytes Sent: {0}", ni.GetIPv4Statistics().BytesSent);
                    Console.WriteLine("    Bytes Received: {0}", ni.GetIPv4Statistics().BytesReceived);

                    Console.WriteLine("    IP Addresses:");
                    foreach (UnicastIPAddressInformation addr in ni.GetIPProperties().UnicastAddresses)
                    {
                        Console.WriteLine("        - {0} (lease expires {1})", addr.Address, DateTime.Now + new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                    }
                }
            }
            else
            {
                Console.WriteLine("No network available.");
            }
        }
    }

Result


Related Tutorials