Detect network card or adapter, changes in C#

In this example, we are going to check the Sysetm.Net.NetworkInformation.NetworkChange Class to have the mechanism to check whether changes to network occurs during the life of your application. Occurding to microsoft's documentation add handlers to NetworkAddressChanged and NetworkAvailabilityChanges events.

Sample output
so lets check the code snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation; 

namespace DetectNetworkChangesCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This sample program will detect network changes in a local computer");
 
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
            
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            
           

            Console.Read();
        }

        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
            {
                Console.WriteLine("Network is available");
            }
            else
            {
                Console.WriteLine("Network is not available");
            }
        }

        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            try
            {
                //check to see if network is available
                if (NetworkInterface.GetIsNetworkAvailable())
                {

                    NetworkInterface[] @interface = NetworkInterface.GetAllNetworkInterfaces();

                    foreach (NetworkInterface face in @interface)
                    {
                        if (face.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                        {
                            Console.WriteLine(face.Name); 
    
                            UnicastIPAddressInformationCollection ip = face.GetIPProperties().UnicastAddresses;

                            foreach (UnicastIPAddressInformation ipp in ip)
                            {
                                Console.WriteLine("Valid Lifetime: {0}", ipp.AddressValidLifetime);
                                Console.WriteLine("Preferred Lifetime: {0}", ipp.AddressPreferredLifetime);
                            }

                            Console.WriteLine("===============================================================");

                        }
                    }

                }

            }
            catch (NetworkInformationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

0 comments:

Post a Comment