Search This Blog

Saturday, March 15, 2014

How to Detect Network Availability and Address Changes in a network

Detect Network Availability and Address Change in Network

We can use the System.Net.NetworkInformation Namespace provides access to network traffic data,network address information,and notification of address changes for the local computer method to all adapters available in the network. System.Net.NetworkInformation also contains the ping utility information.The ping utility is used to check weather a computer is available across the network.

Here is code given to check the network availability ;

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

namespace AddressChanges
{
    public class NetworkingDemo
    {
        public static void Main()
        {
            NetworkChange.NetworkAddressChanged += new 
             NetworkAddressChangedEventHandler(AddressChangedCallback);
            Console.WriteLine("Listening for address changes. Press any key to exit.");
            Console.ReadLine();
        }
        static void AddressChangedCallback(object sender, EventArgs e)
        {
            
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach(NetworkInterface n in adapters)
            {
                Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus);
            }
        }
    }
}

No comments:

Post a Comment