Network - NKS0051

Use NetworkTools.NetworkAvailable

This analyzer provides the following strings:

Context
String

Error List

Caller uses NetworkInterface.GetIsNetworkAvailable instead of NetworkTools.NetworkAvailable

Suggestion Box

Use NetworkTools.NetworkAvailable instead of NetworkInterface.GetIsNetworkAvailable

Description

While NetworkInterface.GetIsNetworkAvailable checks your network, it doesn't work on Android systems, so it's better to use NetworkTools.NetworkAvailable. Please note that this will return false if there is no connectivity for Android systems.

Extended Description

This code analyzer detects the usage of GetIsNetworkAvailable from the NetworkInterface class found in the System.Net.NetworkInformation namespace.

Normally, NetworkInterface.GetIsNetworkAvailable would return true if there are network interfaces that are ready to connect (their state is UP). However, this approach doesn't work on Android systems as Nitrocid doesn't build for Xamarin.Android, which has the necessary libraries for Java bindings to the Android API.

As a result, you'll have to use the sensible NetworkTools.NetworkAvailable property to allow you to check to see if there are ready network interfaces. Please note that if you have no working internet connection, even if your WiFi is turned on in your Android device, it'll report as false.

Analysis Comparison

To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:

Before the fix

Somewhere in your mod code...
public static void MyFunction()
{
    bool available = NetworkInterface.GetIsNetworkAvailable;
}

After the fix

Somewhere in your mod code...
public static void MyFunction()
{
    bool available = NetworkTools.NetworkAvailable;
}

Suppression

You can suppress this suggestion by including it in the appropriate place, whichever is convenient.

For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:

Recommendation

We recommend that every caller which use this function use the recommended abovementioned method.

Last updated