Reputation: 1287
I'm looking for a way to analyze the data consumption from an UWP app. For this, I need to:
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...
Upvotes: 0
Views: 1015
Reputation: 3808
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.
Upvotes: 0