I need a event to detect Internet connect/disconnect

I was able to solve this problem to some extent. I was able to find some sample code in Code project http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=34650. Thanks all for the replies.

especially the article link which was posted by Ms Gregory helped me a lot.


This worked for me!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication6
{


    class Program
    {
        private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {

            if (e.IsAvailable)
                Console.WriteLine("Network connected!");
            else
                Console.WriteLine("Network dis connected!");
        }
        public void Form1()
        {

            NetworkChange.NetworkAvailabilityChanged += AvailabilityChanged;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            p.Form1();

            Console.ReadLine();

        }
    }
}

This is all covered (including the difference between being on the network and having the network connect you to the Internet) at http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx. I hope you meant to put that Windows 7 tag on your post, because all this is pretty new.

The key is INetworkListManager.get_IsConnectedToInternet() which pretty much does what it says on the tin. You have to jump around a bit to register for the events etc. The Code Pack wraps some of that up for you and has a network sample you can adapt.


You can use the NetworkChange class, with the NetworkAvailabilityChanged event:

NetworkChange.NetworkAvailabilityChanged += myNetworkAvailabilityChangeHandler;

Since it's a system event, make sure you delete the event when you're finished, see this post here: You need to be careful about using event handler for NetworkChange