33

We are developing a .NET application where one of the requirements is to monitor whether the system is connected to the internet or not.

We were able to get a .NET event for "ethernet cable disconnect", but if the modem is reset then this event does not get triggered. I don't want to keep pinging some URL to get this done since it will add considerable performance overhead. Is there any .NET event which can check whether the system is connected to the internet or not?

There is an icon in system tray which shows a cross sign or limited connectivity sign when the system is not conncected to the internet. That suggests Windows knows, and I want to trap that event.

Nuetrino
  • 1,099
  • 1
  • 14
  • 32

6 Answers6

27

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

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Thanks for your reply. we are currently the same event for network disconnect. But this event gets triggered only when the Ethernet cable is removed and not when the network goes down due to a modem reset may be. In Windows-7 you can see a system tray icon for Lan Connection which sometimes shows limited connectivity with a ( danger symbol ) it displays No internet access when u do a mouseover. and when Internet is restored then again it reconnects back. – Nuetrino Dec 16 '10 at 10:12
  • 1
    Contd..I was just curious whether that particular event can be trapped. Googled a lot the only replies is to ping the server and see whether it is connected or not and that would be a considerable performance overhead for the application i ruled that out. – Nuetrino Dec 16 '10 at 10:14
  • This "limited connectivity" message is very context-dependent, for example if you're behind many routers. If you really want to know if you are "connected to the internet", you need to define what you mean by that. In the general case, the best is to connect to the end point you're interested in (maybe in another thread with timer). Note also that pings are sometimes prohibited by firewalls. If you need HTTP, you need to test port 80, if you need HTTPS, you need to test 443, etc... – Simon Mourier Dec 16 '10 at 10:33
13

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.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
  • Thank you. That was a good read. I am yet to implement them and check whether it solves my problem. – Nuetrino Dec 17 '10 at 15:38
3

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.

Nuetrino
  • 1,099
  • 1
  • 14
  • 32
1

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();

        }
    }
}
Sandeep Ks
  • 504
  • 3
  • 13
1

try with this:

private void AvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
                Console.WriteLine("Wi-Fi conectado " + DateTime.Now );
            else
                Console.WriteLine("Wi-Fi desconectado " + DateTime.Now);
        }


        public Inicio()
        {
            InitializeComponent();

            NetworkAvailabilityChangedEventHandler myHandler = new NetworkAvailabilityChangedEventHandler(AvailabilityChanged);
            NetworkChange.NetworkAvailabilityChanged += myHandler;
        }
-2

you have to use WMI .

This is example for disconnect detection : ( to test it create a .vbs file and run it )

 strComputer = "."

  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")
  Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from MSNdis_StatusMediaDisconnect") 

  Do While True 
    Set strLatestEvent = colMonitoredEvents.NextEvent 
    Wscript.StdOut.Write "A network connection has been lost: " 
    Wscript.StdOut.Writeline strLatestEvent.InstanceName
  Loop
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
Old Programmer
  • 546
  • 1
  • 4
  • 17