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

后端 未结 27 2026
感动是毒
感动是毒 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:28

    NetworkInterface.GetIsNetworkAvailable is very unreliable. Just have some VMware or other LAN connection and it will return wrong result. Also about Dns.GetHostEntry method I were just concerned about whether test URL might be blocked in the environment where my application going to deploy.

    So another way I found out is using InternetGetConnectedState method. My code is

    [System.Runtime.InteropServices.DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
    
    public static bool CheckNet()
    {
         int desc;
         return InternetGetConnectedState(out desc, 0);         
    }
    

提交回复
热议问题