Get the network usage using GetLocalUsage or GetNetworkUsageAsync

  • Thread starter Thread starter Ashfaque Hussain
  • Start date Start date
A

Ashfaque Hussain

Guest
When I am using GetLocalUsage method then after getting the data usage throwing exception:

Unhandled Exception: System.Exception: Element not found. (Exception from HRESULT: 0x80070490)
at Windows.Networking.Connectivity.ConnectionProfile.GetLocalUsage(DateTimeOffset StartTime, DateTimeOffset EndTime)
at ConsoleApplication2.MainClass.GetNetworkUsage() in d:\userdata\ashfaque\documents\visual studio 2015\Projects\GetLength\GetLength\Program.cs:line 53


If I use GetNetworkUsageAsync method then getting below error:

Unhandled Exception: System.ArgumentException: Value does not fall within the expected range.
at Windows.Foundation.IAsyncOperation`1.GetResults()
at ConsoleApplication2.MainClass.GetNetworkUsage() in d:\userdata\ashfaqque\documents\visual studio 2015\Projects\GetLength\GetLength\Program.cs:line 60



static void GetNetworkUsage()
{
IReadOnlyList<ConnectionProfile> connections = NetworkInformation.GetConnectionProfiles();
ulong DataUsageWeek = 0;
foreach (var connection in connections)
{
if (connection == null) continue;
DataUsageWeek = connection.GetLocalUsage(DateTimeOffset.UtcNow - TimeSpan.FromDays(30), DateTimeOffset.UtcNow).BytesReceived +
connection.GetLocalUsage(DateTimeOffset.UtcNow - TimeSpan.FromDays(24), DateTimeOffset.UtcNow).BytesSent;
Console.WriteLine(DataUsageWeek / 1024);


//var networkUsages = connection.GetNetworkUsageAsync(new DateTimeOffset(DateTime.Now.AddHours(24)), new DateTimeOffset(DateTime.Now), DataUsageGranularity.Total, new NetworkUsageStates()).GetResults();
//foreach (NetworkUsage uses in networkUsages)
//{
// Console.WriteLine(uses.BytesReceived + uses.BytesSent);
//}
}}

Please help me to get the data with any of the above methods.


Thanks,

Ashfaque Hussain

Continue reading...
 
Back
Top