title | author | description | keywords | dev_langs | ||
---|---|---|---|---|---|---|
NetworkHelper |
nmetulev |
The NetworkHelper class provides functionality to monitor changes in network connection and allows users to query for network information without additional lookups. |
windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, NetworkHelper |
|
The NetworkHelper class provides functionality to monitor changes in network connection and allows users to query for network information without additional lookups.
It exposes network information though a property called ConnectionInformation. The ConnectionInformation holds information about ConnectionType, ConnectivityLevel, ConnectionCost, SignalStrength, Internet Connectivity and more.
What is a metered connection? A metered connection is an Internet connection that has a data limit or cost associated with it. Cellular data connections are set as metered by default. Wi-Fi network connections can be set to metered, but aren't by default. Application developers should take metered nature of connection into account and reduce data usage.
[!div class="nextstepaction"] Try it in the sample app
Property | Type | Description |
---|---|---|
ConnectionInformation | ConnectionInformation | Gets instance of ConnectionInformation |
Instance | NetworkHelper | Gets public singleton property |
Property | Type | Description |
---|---|---|
ConnectionCost | ConnectionCost | Gets connection cost for the current Internet Connection Profile |
ConnectionType | ConnectionType | Gets connection type for the current Internet Connection Profile |
ConnectivityLevel | NetworkConnectivityLevel | Gets connectivity level for the current Internet Connection Profile |
IsInternetAvailable | bool | Gets a value indicating whether internet is available across all connections |
IsInternetOnMeteredConnection | bool | Gets a value indicating whether if the current internet connection is metered |
NetworkNames | IReadOnlyList<string> | Gets signal strength for the current Internet Connection Profile |
SignalStrength | Nullable<Byte> | Gets signal strength for the current Internet Connection Profile |
Methods | Return Type | Description |
---|---|---|
UpdateConnectionInformation(ConnectionProfile) | void | Updates the current object based on profile passed |
Events | Description |
---|---|
NetworkChanged | Event raised when the network changes |
// Detect if Internet can be reached
if (NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
}
// Detect if the connection is metered
if (NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection)
{
}
// Get precise connection type
switch(NetworkHelper.Instance.ConnectionInformation.ConnectionType)
{
case ConnectionType.Ethernet:
// Ethernet
break;
case ConnectionType.WiFi:
// WiFi
break;
case ConnectionType.Data:
// Data
break;
case ConnectionType.Unknown:
// Unknown
break;
}
' Detect if Internet can be reached
If NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable Then
...
End If
' Detect if the connection is metered
If NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection Then
...
End If
' Get precise connection type
Select Case NetworkHelper.Instance.ConnectionInformation.ConnectionType
Case ConnectionType.Ethernet
' Ethernet
Case ConnectionType.WiFi
' WiFi
Case ConnectionType.Data
' Data
Case ConnectionType.Unknown
' Unknown
End Select
NetworkHelper sample page Source. You can see this in action in the Windows Community Toolkit Sample App.
Device family | Universal, 10.0.16299.0 or higher |
---|---|
Namespace | Microsoft.Toolkit.Uwp.Connectivity |
NuGet package | Microsoft.Toolkit.Uwp.Connectivity |