There is no "right way", although the use of network interfaces can be really useful and valid, it is not 100% reliable, since it is possible that network interfaces vary from user to user.
What I usually do is a "cascade" of attempts.
The first attempt is through a Windows DLL that is imported into the code:
[System.Runtime.InteropServices.DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(out int Description, int ReservedValue);
Then we make the call like this:
int desc;
hasConnection = InternetGetConnectedState(out desc, 0);
The second way is using the ping
from Windows itself, but note that it is possible that Ping may not exist or fail on certain computers (yet it is more reliable than listing networks)
//Segunda verificação, executa ping
hasConnection = (new Ping().Send("www.google.com").Status == IPStatus.Success) ? true : false;
In the latter case we used the same medium that Oce used:
//Checa adaptadores de rede
if (NetworkInterface.GetIsNetworkAvailable())
{
//Busca todos os adaptadores de rede
foreach (NetworkInterface network in NetworkInterface.GetAllNetworkInterfaces())
{
if (network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType != NetworkInterfaceType.Tunnel && network.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
hasConnection = true;
}
else
{
hasConnection = false;
}
}
}
Ai believe that the ideal version would chain the three in a decision block and check one at a time if the previous one does not exist.
One possibility is to use the class Ping with the ips
8.8.8.8
and/or8.8.4.4
(Google DNS servers). Other possibilities (in English) on Superuser– Gomiero