Mohit Deshpande Mohit Deshpande - 2 months ago 13
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

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;
    }
}
Comments