UWP/C# Get the data usage per network type and per app

960 Views Asked by At

I'm looking for a way to analyze the data consumption from an UWP app. For this, I need to:

  • get the data usage per newtork (wifi, cellular, roaming, ...)
  • get the detailed information per installed app

In Windows 10, we can found these informations easily:

But how to get the same information in an app?

The only closest topic I foud is this one, but it's not really the same thing...

1

There are 1 best solutions below

0
Breeze Liu - MSFT On

In UWP, We can get the network usage from a network connection using the ConnectionProfile Class which provides information about the connection status and connectivity statistics.

For your data usage, you can try the following code,

try
{
    var ConnectionProfiles = NetworkInformation.GetConnectionProfiles();
    if (ConnectionProfiles != null)
    {
        Debug.WriteLine(ConnectionProfiles.Count);
        foreach (var profile in ConnectionProfiles)
        {
            //Profile name
            var name = profile.ProfileName;

            NetworkUsageStates networkUsageStates = new NetworkUsageStates();
            networkUsageStates.Roaming = TriStates.DoNotCare;
            networkUsageStates.Shared = TriStates.DoNotCare;
            //get the data usage from the last 30 day
            DateTime startTime = DateTime.Now.AddDays(-30);
            DateTime endTime = DateTime.Now;
            var usages = await profile.GetNetworkUsageAsync(startTime,
                endTime, DataUsageGranularity.Total, networkUsageStates);
            if (usages != null)
            {
                foreach (var use in usages)
                {
                    //Data usage.
                    var TotalDataUsage = use.BytesReceived + use.BytesSent;
                }
            }
        }
    }
}
catch (Exception e)
{
    Debug.WriteLine(e.Message);
}

You can configure different DataUsageGranularity and NetworkUsageStates to get the data usage you want.

But for your usage details, since UWP app run sandboxed, you can not get the detailed information, and there are no corresponding APIs in UWP.