Windows 10 UWP - C#:如何检查网络类型(EDGE/3G/LTE),不仅是蜂窝网络还是 WLAN? [英] Windows 10 UWP - C#: How to check network type (EDGE/3G/LTE), not only cellular vs. WLAN?
问题描述
如何查看当前使用的是哪种蜂窝网络连接 (EDGE/3G/LTE)?我只知道如何检查设备是使用蜂窝连接还是 WLAN,但我需要特定类型的蜂窝连接.谢谢!
How can I check which type of cellular connection (EDGE/3G/LTE) is used at the moment? I only know how to check if device is on cellular connection or WLAN, but I need specific type of cellular connection. Thank you!
推荐答案
NetworkInformation 应该可用于 Windows 10 (https://msdn.microsoft.com/en-us/library/windows.networking.connectivity.networkinformation.getinternetconnectionprofile.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2).
NetworkInformation should be available for Windows 10 (https://msdn.microsoft.com/en-us/library/windows.networking.connectivity.networkinformation.getinternetconnectionprofile.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2).
您必须根据需要调整此代码:
You have to adjust this code for your needs:
/// <summary>
/// ("Connection Type", `0`-`3`): `0` - cellular, `1` - wifi / ethernet, `2` - inne;
/// </summary>
/// <returns></returns>
public byte GetConnectionGeneration()
{
try
{
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
if (profile.IsWwanConnectionProfile)
{
WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass();
switch (connectionClass)
{
//2G-equivalent
case WwanDataClass.Edge:
case WwanDataClass.Gprs:
//3G-equivalent
case WwanDataClass.Cdma1xEvdo:
case WwanDataClass.Cdma1xEvdoRevA:
case WwanDataClass.Cdma1xEvdoRevB:
case WwanDataClass.Cdma1xEvdv:
case WwanDataClass.Cdma1xRtt:
case WwanDataClass.Cdma3xRtt:
case WwanDataClass.CdmaUmb:
case WwanDataClass.Umts:
case WwanDataClass.Hsdpa:
case WwanDataClass.Hsupa:
//4G-equivalent
case WwanDataClass.LteAdvanced:
return 0;
//not connected
case WwanDataClass.None:
return 2;
//unknown
case WwanDataClass.Custom:
default:
return 2;
}
}
else if (profile.IsWlanConnectionProfile)
{
return 1;
}
return 2;
}
catch (Exception)
{
return 2; //as default
}
}
这篇关于Windows 10 UWP - C#:如何检查网络类型(EDGE/3G/LTE),不仅是蜂窝网络还是 WLAN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!