Search code examples
c#windows-phone-8network-connection

Check if Internet is turned on in windows 8 phone app?


I have a Windows Phone 8 application. I need to check whether the device has internet connection before my application is launched or opened.

If Internet is off then it should direct the user to the settings page to turn on the internet.

public static bool checkNetworkConnection()
{
    var ni = NetworkInterface.NetworkInterfaceType;

    bool IsConnected = false;
    if ((ni == NetworkInterfaceType.Wireless80211)|| (ni == NetworkInterfaceType.MobileBroadbandCdma)|| (ni == NetworkInterfaceType.MobileBroadbandGsm))
        IsConnected= true;
    else if (ni == NetworkInterfaceType.None)
        IsConnected= false;
    return IsConnected;
}

I tried this but it says:

NetworkInterfaceType not found in System.Net.NetworkInformation

can someone please guide me with this.


Solution

  • Please try below code:

    public static bool checkNetworkConnection()
    {
        return NetworkInterface.GetIsNetworkAvailable();
    }
    

    Use this Namespace: using Microsoft.Phone.Net.NetworkInformation; for get network information.

    Hope this helpful to you.