4

I need a way to determine internet availability programmatically. At now i use Ping to constantly ping some internet site.

But it seems Windows 7 though determines internet availability in some other way. If computer is online there is earth icon on the network interface icon in the System Tray.

The question is: is there any standard Win32 way to check online status, win event or something, and if so, how to use it from C#?

skaeff
  • 753
  • 2
  • 13
  • 25
  • What are you going to do with this information? Since the internet connection might go down at any moment, you have to program defensively against that anyway. – Damien_The_Unbeliever Oct 13 '10 at 09:54
  • Whell, i have a code which reenables LAN interface. – skaeff Oct 13 '10 at 10:18
  • It disables LAN interface through WMI and then enables again - when there are some problems on the provider side it helps. I also plan to set a WiMax or GSM modem, because it is very critical for my app to be connected :) – skaeff Oct 13 '10 at 10:18

2 Answers2

4

I believe something like this would work although your question appears to be a duplicate:

using System;
using System.Runtime;
using System.Runtime.InteropServices;

public class InternetCS
{
    //Creating the extern function...
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );

    //Creating a function that uses the API function...
    public static bool IsConnectedToInternet( )
    {
        int Desc ;
        return InternetGetConnectedState( out Desc, 0 ) ;
    }
}

forund here:

check whether Internet connection is available with C#

Community
  • 1
  • 1
Michael Eakins
  • 4,149
  • 3
  • 35
  • 54
2

'Connected to the Internet' doesn't have any actual meaning except in the case of a modem. The beat way to test whether any resource is available is to use it. You have to cope with failures at that point anyway, no need to code everything twice.

bitbonk
  • 48,890
  • 37
  • 186
  • 278
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Well, it is already done so. Every 3 seconds i send ping to a public whell-known host and if i fail for at least 10 cycles - i reboot network interface. – skaeff Oct 13 '10 at 12:10