Windows 10 UWP - 检测当前的Internet连接是Wifi还是Cellular?

Sla*_*bal 20 c# windows-store-apps windows-10-mobile uwp

在Windows 10 UWP应用程序中,如何检测当前的Internet连接是Wifi还是Cellular?

小智 23

在UWP中,您可以使用IsWlanConnectionProfile或IsWwanConnectionProfile属性检查网络连接.

一个例子是:

var temp = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();

if (temp.IsWlanConnectionProfile)
{
     // its wireless
}else if (temp.IsWwanConnectionProfile)
{
     // its mobile
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

  • 你应该检查`temp`是否为'null`. (2认同)

Igo*_*lic 10

除了获得连接(其他人提到)之外,您还可以更好地处理计量连接.

如何管理计量网络成本约束

switch (connectionCost.NetworkCostType)
{
    case NetworkCostType.Unrestricted:
        //
        break;
    case NetworkCostType.Fixed:
        //
        break;
    case NetworkCostType.Variable:
        //
        break;
    case NetworkCostType.Unknown:
        //
        break;
    default:
        //
        break;
}
Run Code Online (Sandbox Code Playgroud)

请参阅GitHub上网络演示.

if (connectionCost.Roaming || connectionCost.OverDataLimit)
{
    Cost = NetworkCost.OptIn;
    Reason = connectionCost.Roaming
        ? "Connection is roaming; using the connection may result in additional charge."
        : "Connection has exceeded the usage cap limit.";
}
else if (connectionCost.NetworkCostType == NetworkCostType.Fixed
    || connectionCost.NetworkCostType == NetworkCostType.Variable)
{
    Cost = NetworkCost.Conservative;
    Reason = connectionCost.NetworkCostType == NetworkCostType.Fixed
        ? "Connection has limited allowed usage."
        : "Connection is charged based on usage. ";
}
else
{
    Cost = NetworkCost.Normal;
    Reason = connectionCost.NetworkCostType == NetworkCostType.Unknown
        ? "Connection is unknown"
        : "Connection cost is unrestricted";
}
Run Code Online (Sandbox Code Playgroud)