Skip to main content

There is no way you can reliably check if there is an internet connection, but we can come close.

/// <summary>
/// There is no way you can reliably check if there is an internet connection,
/// but we can come close.
/// </summary>
public static bool HasInternetConnection
{
    get
    {
        var result = false;

        try
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                using (var p = new Ping())
                {
                    result = (p.Send("8.8.8.8", 15000).Status == IPStatus.Success) ||
                             (p.Send("8.8.4.4", 15000).Status == IPStatus.Success) ||
                             (p.Send("4.2.2.1", 15000).Status == IPStatus.Success);
                }
            }
        }
        catch {}

        return result;
    }
}