UWP development: Get the user's current network environment (WIFI, mobile network, LAN ...) )

Source: Internet
Author: User

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 ...) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.