Network Access Detection
Detecting the network connectivity status of a device is essential for building resilient applications. .NET MAUI provides the Connectivity
class to query the current network state and respond to changes.
Key Features
- Check if the device is connected to any network.
- Identify the type of connection (Wi‑Fi, Cellular, Ethernet, etc.).
- Subscribe to connectivity change events.
- Retrieve detailed network access information.
Getting Started
First, add the required namespace:
using Microsoft.Maui.Networking;
Check Current Connectivity
bool isConnected = Connectivity.Current.NetworkAccess == NetworkAccess.Internet;
if (isConnected)
{
// Device has internet access
}
else
{
// No internet
}
Retrieve Connection Profiles
IReadOnlyList profiles = Connectivity.Current.ConnectionProfiles;
foreach (var profile in profiles)
{
Console.WriteLine(profile);
}
Subscribe to Changes
Connectivity.Current.ConnectivityChanged += (sender, args) =>
{
var access = args.NetworkAccess;
var profiles = args.ConnectionProfiles;
// React to changes
};
Sample App
Below is a minimal page that shows the current status and updates automatically.
public partial class NetworkStatusPage : ContentPage
{
Label _statusLabel;
public NetworkStatusPage()
{
_statusLabel = new Label { FontSize = 18, HorizontalOptions = LayoutOptions.Center };
Content = new StackLayout
{
Padding = new Thickness(20),
Children = { _statusLabel }
};
UpdateStatus();
Connectivity.Current.ConnectivityChanged += (s, e) => UpdateStatus();
}
void UpdateStatus()
{
var access = Connectivity.Current.NetworkAccess;
var profiles = string.Join(", ", Connectivity.Current.ConnectionProfiles);
_statusLabel.Text = $"Access: {access}\\nProfiles: {profiles}";
}
}