Why is GetIsNetworkAvailable() always returning true?

后端 未结 6 686
醉梦人生
醉梦人生 2020-12-16 14:57

I have this method:

public static void testConnection()
    {
        if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
          


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 15:33

    Please correct me if I am wrong but as far as I can see the method you are using is checking network connectivity and not necessarily internet connectivity. I would assume if you are on a network of any sort this would return true regardless of the internet being available or not? See this.

    I have noticed that one way of checking for internet connectivity is as follows:

    private bool IsInternetAvailable()
    {
        try
        {
            Dns.GetHostEntry("www.google.com"); //using System.Net;
            return true;
        } catch (SocketException ex) {
            return false;
        }
    }
    

    The above code can be found (in VB.Net by reading the comment from Joacim Andersson [MVP]) in the following post.

    Note: The latest edit was suggested by AceInfinity but was rejected in community review. My reputation is too low to override this so I made the change myself.

提交回复
热议问题