Pergunta

Is there any better way to check a Working Internet Connection than the following code because it always returns true, I don't know why.

bool isNetwork=NetworkInterface.GetIsNetworkAvailable();
if(!isNetwork)
{
   MessageBox.Show("Available.");
}
Foi útil?

Solução

GetIsNetworkAvailable returns a result based on the network interfaces available. You should turn off all radios or turn on flight mode to get this to return false.

You may also want to check:

NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.None

In reality though, what you probably want to do is check if you can reach a specific URI. Just knowing if you can connect to "the internet" doesn't help you if there is a proxy between you and the endpoint that will block the connection or even if the server you're trying to reach is down or experiencing errors.

If you want to know if you can retrieve the data you desire from a specific endpoint, the only way to know for sure is to try. If you're successful then you can. If not there could be numerous reasons why.

Outras dicas

Try this also,

 public static bool CheckNetworkConnection()
    {
        var networkInterface = NetworkInterface.NetworkInterfaceType;

        bool isConnected = false;
        if ((networkInterface == NetworkInterfaceType.Wireless80211)||(networkInterface== NetworkInterfaceType.MobileBroadbandGsm)||(networkInterface==NetworkInterfaceType.MobileBroadbandCdma))
            isConnected = true;

        else if (networkInterface == NetworkInterfaceType.None)
            isConnected = false;
        return isConnected;
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top