Listening for Network Address Changes

In version 2.0 of the .Net Framework, we added a new namespace under System.Net called NetworkInformation.  Among many other goodies, one can use this namespace to listen for IP addresses changes on the host.  The code below listens for addresses changes and prints a message when a change occurs along with the operational status of each interface on the host:

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

namespace Examples.Net.AddressChanges
{
    public class NetworkingExample
    {
        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)
        {
            Console.WriteLine("Addresses Change Event Raised");
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach(NetworkInterface n in adapters)
            {
                Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus);
            }
        }
    }
}

NOTE: Due to timing in the underlying operating system, if you plan to inspect what addresses are available as soon as the callback fires, you may need to sleep for 1 second.  If you don't sleep, when you inspect the current set of addresses the changes may not have taken effect.