What is the fastest and most efficient way to check for Internet connectivity in .NET?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
I personally find the answer of Anton and moffeltje best, but I added a check to exclude virtual networks set up by VMWare and others.
NetworkInterface.GetIsNetworkAvailable
is very unreliable. Just have some VMware or other LAN connection and it will return wrong result. Also aboutDns.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 isHere's how it is implemented in Android.
As a proof of concept, I translated this code to C#:
There is absolutely no way you can reliably check if there is an internet connection or not (I assume you mean access to the internet).
You can, however, request resources that are virtually never offline, like pinging google.com or something similar. I think this would be efficient.