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

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

    I have seen all the options listed above and the only viable option to check wither the internet is available or not is the "Ping" option. Importing [DllImport("Wininet.dll")] and System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() Or any other variation of the NetworkInterface class does not work well in detecting the availability of the network.These Methods only check if the network cable is plugged in or not.

    The "Ping option"

    if(Connection is available) returns true

    if(Connection is not available and the network cable is plugged in) returns false

    if(Network cable is not plugged in) Throws an exception

    The NetworkInterface

    if(Internet Is available)Returns True

    if(Internet is not Available and Network Cable is Plugged in ) Returns True

    if(Network Cable is Not Plugged in )returns false

    The [DllImport("Wininet.dll")]

    if(Internet Is available)Returns True

    if(Internet is not Available and Network Cable is Plugged in ) Returns True

    if(Network Cable is Not Plugged in )returns false

    So in case of [DllImport("Wininet.dll")] and NetworkInterface There is no way of knowing if internet connection is available.

提交回复
热议问题