Accessing Network Profile Information in Windows Store Apps Using C#

Today I will show you how to retrieve network connection profile information with Windows Store apps. The Windows Runtime provides a set of APIs that allow an app to get the network connectivity information.

In this article I will implement some methods for getting the required information about network status, connection profiles etc. Network connection information includes the connection Cost, Cost type, Data plan Status and Usage, Network Adapter etc.

First of all we have to know about the connection profile. The Connection profile represents the single network connection on a device. We can access many of the information using the connection profile object such as connectivity level, data usage, cost of network etc.

In the later section we will learn how to access connectivity information with Windows Store Apps using XAML/C#.

Here I will use the NetworkInformation class to retrieve the data on each connection profile. It is a static class and has many properties and methods.

To access the this class you will need to include the correct namespace, as in:

using Windows.Networking.Connectivity;

To get the Connection Profile Information I use the GetInternetConnectionProfile method of the NetworkInformation class. It returns the ConnectionProfile object that is currently used to connect to the internet; see:

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

The ConnectionProfile object has the following methods or properties to access the network information:

  • GetConnectionCost(): Provides connection cost information details, including data limit and roaming information.
  • GetDataPlanStatus(): Provides usage information specific to the data plan associated with the connection.
  • GetLocalUsage(): Provides local connection usage information.
  • GetNetworkConnectivityLevel(): Get the network connectivity level for the current connection.
  • NetworkSecuritySettings: Reterive all the security setting for the network.
  • ProfileName: Gets the name of the connection profile.

We use these methods below:

Get the profile name for the connection.

connectionProfileInfo = "Profile Name : " + connectionProfile.ProfileName + "\n";

Getting the Network connectivity level for the connection:
 

switch (connectionProfile.GetNetworkConnectivityLevel())

{

    case NetworkConnectivityLevel.None:

    connectionProfileInfo += "Connectivity Level : None\n";

     break;

     case NetworkConnectivityLevel.LocalAccess:

     connectionProfileInfo += "Connectivity Level : Local Access\n";

     break;

     case NetworkConnectivityLevel.ConstrainedInternetAccess:

     connectionProfileInfo += "Connectivity Level : Constrained Internet Access\n";

     break;

     case NetworkConnectivityLevel.InternetAccess:

     connectionProfileInfo += "Connectivity Level : Internet Access\n";

     break;

}

Here I retrieve the connection cost and data plan status information for the current connection profile; see:
 

string GetDataPlanStatusInfo(DataPlanStatus dataPlan)

{

    string dataplanStatusInfo = string.Empty;

    dataplanStatusInfo = "Dataplan Status Information:\n";

    dataplanStatusInfo += "====================\n";

 

    if (dataPlan == null)

    {

        dataplanStatusInfo += "Dataplan Status not available\n";

        return dataplanStatusInfo;

    }

           

    if (dataPlan.DataPlanUsage != null)

    {

         dataplanStatusInfo += "Usage In Megabytes : " + dataPlan.DataPlanUsage.MegabytesUsed + "\n";

         dataplanStatusInfo += "Last Sync Time : " + dataPlan.DataPlanUsage.LastSyncTime + "\n";

    }

    else

    {

       dataplanStatusInfo += "Usage In Megabytes : Not Defined\n";

    }

 

    ulong? inboundBandwidth = dataPlan.InboundBitsPerSecond;

    if (inboundBandwidth.HasValue)

    {

       dataplanStatusInfo += "InboundBitsPerSecond : " + inboundBandwidth + "\n";

    }

    else

    {

       dataplanStatusInfo += "InboundBitsPerSecond : Not Defined\n";

    }

 

    ulong? outboundBandwidth = dataPlan.OutboundBitsPerSecond;

    if (outboundBandwidth.HasValue)

    {

        dataplanStatusInfo += "OutboundBitsPerSecond : " + outboundBandwidth + "\n";

    }

    else

    {

       dataplanStatusInfo += "OutboundBitsPerSecond : Not Defined\n";

    }

 

    uint? dataLimit = dataPlan.DataLimitInMegabytes;

    if (dataLimit.HasValue)

    {

       dataplanStatusInfo += "DataLimitInMegabytes : " + dataLimit + "\n";

    }

    else

    {

       dataplanStatusInfo += "DataLimitInMegabytes : Not Defined\n";

    }

 

    System.DateTimeOffset? nextBillingCycle = dataPlan.NextBillingCycle;

    if (nextBillingCycle.HasValue)

    {

       dataplanStatusInfo += "NextBillingCycle : " + nextBillingCycle + "\n";

    }

    else

    {

       dataplanStatusInfo += "NextBillingCycle : Not Defined\n";

    }

 

    uint? maxTransferSize = dataPlan.MaxTransferSizeInMegabytes;

    if (maxTransferSize.HasValue)

    {

        dataplanStatusInfo += "MaxTransferSizeInMegabytes : " + maxTransferSize + "\n";

    }

    else

    {

       dataplanStatusInfo += "MaxTransferSizeInMegabytes : Not Defined\n";

    }

      return dataplanStatusInfo;

}


Here is the output:

Accessing-Network-Profile-Information-In-Windows-Store-apps.png

Summary

We leared how to retrieve connection cost and data plan status information for a specific connection profile.
  


Similar Articles