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

Mohit Deshpande picture Mohit Deshpande · Jan 9, 2010 · Viewed 244.5k times · Source

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

Answer

ChaosPandion picture ChaosPandion · Jan 9, 2010

Something like this should work.

System.Net.WebClient

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