Mohit Deshpande Mohit Deshpande - 1 year ago 65
C# Question

What is the best way to check for Internet connectivity using .NET?

What is the fastest and most efficient way to check for Internet connectivity in .NET?

Answer Source

Something like this should work.

System.Net.WebClient

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        {
            using (var stream = client.OpenRead("http://www.google.com"))
            {
                return true;
            }
        }
    }
    catch
    {
        return false;
    }
}