Hello,
Welcome to Microsoft Q&A!
You could take a look at the NetworkInformation Class. You could use this class to access network connection information for the local machine. It contains a NetworkStatusChanged Event that occurs when the network status changes for a connection. You could check the connection profile in the event to check if the network is wifi or mobile or LAN, check if the network is available.
The code looks like this:
private NetworkStatusChangedEventHandler networkStatusCallback;
private Boolean registeredNetworkStatusNotif;
void NetworkStatusChange()
{
// register for network status change notifications
try
{
networkStatusCallback = new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
if (!registeredNetworkStatusNotif)
{
NetworkInformation.NetworkStatusChanged += networkStatusCallback;
registeredNetworkStatusNotif = true;
}
}
catch (Exception ex)
{
}
}
public async void OnNetworkStatusChange(object sender)
{
try
{
// get the ConnectionProfile that is currently used to connect to the Internet
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (InternetConnectionProfile == null)
{
// not connect
}
else
{
// check if it is WiFi or it is mobile. otherwise it is LAN
bool isWLANConnection = (InternetConnectionProfile == null) ? false : InternetConnectionProfile.IsWlanConnectionProfile;
bool isWWANConnection = (InternetConnectionProfile == null) ? false : InternetConnectionProfile.IsWwanConnectionProfile;
// check if network is enabled
bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();
}
}
catch (Exception ex)
{
}
}
You could refer to these documents for more information here: How to retrieve network connection information and How to manage network connection events and changes in availability.
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.