UWP Development: Get the network environment where the user is currently located:
In UWP development, sometimes, we need to determine the user's network, is WiFi, or mobile network, to remind users to avoid the loss of user traffic, the UWP provides the user's network of some information, but the specific type, we need further judgment processing. As follows
This static Class I have already written, the use of the time just call its getconnectiongeneration () method to return the current network type. The types that can be detected are 2G, 3G, 4G, WiFi, LAN, and so on. Finally, let's give you the code. At the same time, welcome the students who love UWP development to join the group: 193148992 Exchange discussion.
public static class Internetstatus {static string none = "None"; static string Unknown = "Unknown"; static string IIG = "2G"; static string Iiig = "3G"; static string IVG = "4G"; static string WiFi = "WiFi"; static string lan = "LAN"; public static string Getconnectiongeneration () {bool isconnected = false; string internettype = null; Connectionprofile profile = Networkinformation.getinternetconnectionprofile (); if (profile = = null) {Internettype = Internetstatus.none; } else {Networkconnectivitylevel cl = profile. Getnetworkconnectivitylevel (); IsConnected = (CL! = Networkconnectivitylevel.none); } if (!isconnected) {return internetstatus.none; } if (profile. Iswwanconnectionprofile) {if (Profile. Wwanconnectionprofiledetails = = null) {Internettype = Internetstatus.unknown; } wwandataclass Connectionclass = profile. Wwanconnectionprofiledetails.getcurrentdataclass (); Switch (connectionclass) {//2g case Wwandataclass.edge: Case WwanDataClass.Gprs:InternetType = Internetstatus.iig; Break 3G Case WwanDataClass.Cdma1xEvdo:case Wwandataclass.cdma1xevdoreva: Case WwanDataClass.Cdma1xEvdoRevB:case WwanDataClass.Cdma1xEvdv:case Wwandat AClass.Cdma1xRtt:case WwanDataClass.Cdma3xRtt:case Wwandataclass.cdmaumb: Case WwanDataClass.Umts:case WwanDataClass.Hsdpa:case WwandataClass.Hsupa:InternetType = Internetstatus.iiig; Break 4G case WwanDataClass.LteAdvanced:InternetType = INTERNETSTATUS.IVG; Break No net case WwanDataClass.None:InternetType = Internetstatus.unknown; Break Case WwanDataClass.Custom:default:InternetType = Internetstatus.unknown; Break }} else if (profile. Iswlanconnectionprofile) {internettype = Internetstatus.wifi; } else {///not WiFi nor cellular data is judged as Lan internettype = Internetstatus.lan; } return Internettype; } }
--it Chase Dream Garden
UWP development: Get the user's current network environment (WIFI, mobile network, LAN ...) )