NetworkInformation Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides access to network connection information for the local machine.
public ref class NetworkInformation abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class NetworkInformation final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public static class NetworkInformation
Public Class NetworkInformation
- Inheritance
- Attributes
Windows requirements
| Device family |
Windows 10 (introduced in 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
|
| API contract |
Windows.Foundation.UniversalApiContract (introduced in v1.0)
|
Examples
Event subscription pattern (C#):
using Windows.Networking.Connectivity;
NetworkStatusChangedEventHandler handler = sender =>
{
var profile = NetworkInformation.GetInternetConnectionProfile();
var level = profile?.GetNetworkConnectivityLevel();
if (level == NetworkConnectivityLevel.InternetAccess)
{
// Safe to (re)try outbound requests
}
};
NetworkInformation.NetworkStatusChanged += handler;
Selective re-query pattern (C# pseudo-code):
void OnNetworkStatusChanged(NetworkStateChangeEventDetails details)
{
var profile = NetworkInformation.GetInternetConnectionProfile();
if (details.HasNewNetworkConnectivityLevel || details.HasNewInternetConnectionProfile)
{
var level = profile?.GetNetworkConnectivityLevel();
}
if (details.HasNewConnectionCost)
{
var cost = profile?.GetConnectionCost();
}
if (details.HasNewDomainConnectivityLevel)
{
bool tls = profile?.IsDomainAuthenticatedBy(DomainAuthenticationKind.Tls) == true;
}
if (details.HasNewHostNameList)
{
// Refresh host name dependent routing logic
}
}
Filtering example (C#):
using Windows.Networking.Connectivity;
var filter = new ConnectionProfileFilter
{
IsConnected = true,
IsWlanConnectionProfile = true
};
var wifiProfiles = await NetworkInformation.FindConnectionProfilesAsync(filter);
Determine if the device currently has internet access and is on an unrestricted network (C#):
using Windows.Networking.Connectivity;
var profile = NetworkInformation.GetInternetConnectionProfile();
bool hasInternet = profile?.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
bool unrestricted = false;
if (profile != null)
{
var cost = profile.GetConnectionCost();
unrestricted = cost.NetworkCostType == NetworkCostType.Unrestricted && !cost.Roaming && !cost.OverDataLimit;
}
// Use hasInternet/unrestricted to decide whether to start background sync
Enumerate all connected WLAN profiles (C++/WinRT):
#include <winrt/Windows.Networking.Connectivity.h>
using namespace winrt;
using namespace Windows::Networking::Connectivity;
IAsyncAction ListWifiProfiles()
{
ConnectionProfileFilter filter;
filter.IsWlanConnectionProfile(true);
filter.IsConnected(true);
auto profiles = co_await NetworkInformation::FindConnectionProfilesAsync(filter);
for (auto const& p : profiles)
{
if (auto wlan = p.WlanConnectionProfileDetails())
{
auto ssid = wlan.GetConnectedSsid();
// Log or display ssid
}
}
}
Subscribe to network status change (C++/WinRT):
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Networking.Connectivity.h>
using namespace winrt;
using namespace Windows::Networking::Connectivity;
struct NetworkWatcher
{
winrt::event_token token;
void Start()
{
token = NetworkInformation::NetworkStatusChanged({ this, &NetworkWatcher::OnChanged });
}
void Stop()
{
if (token.value) NetworkInformation::NetworkStatusChanged(token);
}
void OnChanged(IInspectable const&)
{
auto profile = NetworkInformation::GetInternetConnectionProfile();
if (profile && profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel::InternetAccess)
{
// Online logic
}
}
};
Remarks
NetworkInformation provides static methods to query network connectivity state and monitor changes:
- Call NetworkInformation.GetInternetConnectionProfile to get the current active connection profile (may return null if offline).
- Call NetworkInformation.FindConnectionProfilesAsync with a ConnectionProfileFilter to enumerate additional profiles (for example, other WLAN interfaces, WWAN, or prior connections).
- Subscribe to the NetworkInformation.NetworkStatusChanged event to be notified when connectivity changes instead of polling.
The returned ConnectionProfile instances expose cost, data plan, adapter, and technology-specific detail objects (WlanConnectionProfileDetails, WwanConnectionProfileDetails). Always re-query inside the status changed event handler because previously cached profile objects are not live-updating.
For examples of how NetworkInformation class methods are implemented, see the
NetworkConnectivity sample.
Use this class to:
- Retrieve LAN/WLAN/WWAN specific details (for example, WlanConnectionProfileDetails, WwanConnectionProfileDetails).
- Obtain localized names or signal / data plan information through the associated profile objects.
Connection attempt best practices
Important
The network status reported by Windows APIs is only a hint - its accuracy may vary depending on the local network
topology and conditions. Apps should attempt to connect to their services whenever LocalAccess or higher connectivity
is reported.
For a complete implementation demonstrating these principles, see the NetworkConnectivity sample.
Additional guidance
- If your scenario depends on cost awareness (metered vs unrestricted), query
connectionCost = profile?.GetConnectionCost()and checkconnectionCost.NetworkCostTypebefore large background transfers. - For power efficiency, unsubscribe from events when your foreground component is not active.
- Connectivity level can upgrade (for example from
ConstrainedInternetAccesstoInternetAccess) without the internet profile reference changing. Re-checkGetNetworkConnectivityLevelinside each event invocation.
Event handling best practices:
Important
Always re-query GetInternetConnectionProfile inside the event handler. Do not cache an old profile instance and assume it's updated automatically.
- The event can fire frequently (for example, captive portal transitions, cost policy changes). Keep handlers lightweight and debounce expensive work.
- If using background tasks with
NetworkStateChangeEventDetails, inspect flags (HasNewConnectionCost,HasNewNetworkConnectivityLevel,HasNewDomainConnectivityLevel, etc.) to selectively re-query only what changed.
Related classic (Win32) technologies include Network List Manager (NLM / INetworkListManager) and Data Usage & Subscription Management (DUSM). Most UWP / WinRT apps should prefer NetworkInformation and ConnectionProfile over directly invoking classic APIs; consult classic samples only for desktop bridge or advanced diagnostics scenarios.
For broader scenario guidance, see:
Methods
| FindConnectionProfilesAsync(ConnectionProfileFilter) |
Returns an array of ConnectionProfile objects that match the filtering criteria defined by ConnectionProfileFilter. |
| GetConnectionProfiles() |
Enumerates all connection profiles (active or not) currently known to the system for the local machine. |
| GetHostNames() |
Gets a list of host names associated with the local machine. |
| GetInternetConnectionProfile() |
Retrieves the connection profile associated with the preferred interface currently used by the local machine. The preferred interface is the one most likely to send or receive internet traffic. This means that the returned profile might or might not have internet access. |
| GetLanIdentifiers() |
Important Location consent is now required for Wi-Fi BSSID access. For details on how this affects apps using Wi-Fi or ___location APIs, see Changes to API behavior for Wi-Fi access and ___location. Gets an array of LanIdentifier objects that contain locality information for each NetworkAdapter object that currently connected to a network. |
| GetProxyConfigurationAsync(Uri) |
Gets proxy configuration for a connection using the specified URI. |
| GetSortedEndpointPairs(IIterable<EndpointPair>, HostNameSortOptions) |
Gets a sorted list of EndpointPair objects. |
Events
| NetworkStatusChanged |
Occurs when the network status changes for a connection. |