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

后端 未结 27 2464
感动是毒
感动是毒 2020-11-22 07:41

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

27条回答
  •  再見小時候
    2020-11-22 08:31

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

提交回复
热议问题