0

I love programming for UWP and I start new program with C# and UWP. I want to get network usage for each connection in the target system. I read document in MSDN and I write this code for my app:

public async static Task<List<ConnectionReport>> GetNetworkUsageAsync(string ConnectionName, DateTimeOffset StartTime, DateTimeOffset EndTime, DataUsageGranularity Granularity)
{
    var granularityTimeSpan = GranularityToTimeSpan(Granularity);
    try
    {
        var connectionProfile = GetConnectionProfile(ConnectionName);
        var networkUsages = await connectionProfile.GetNetworkUsageAsync(StartTime, EndTime, Granularity, new NetworkUsageStates());
        List<ConnectionReport> listNetworkUsage = new List<ConnectionReport>();
        foreach (var networkUsage in networkUsages)
        {
            var connectionReport = new ConnectionReport()
            {
                Date = StartTime.Date.ToString(dateFormat),
                Download = ConvertBytesToMB(networkUsage.BytesReceived),
                Upload = ConvertBytesToMB(networkUsage.BytesSent),
            };                                     
            listNetworkUsage.Add(connectionReport);
            StartTime = StartTime.Add(granularityTimeSpan);
        }
        return listNetworkUsage;
    }
    catch (Exception ex)
    {                
        // do something
    } 
}         

private static ConnectionProfile GetConnectionProfile(string ConnectionName)
{
    var connectionProfiles = NetworkInformation.GetConnectionProfiles();
    foreach (var connection in connectionProfiles)
    {
        if (connection.ProfileName == ConnectionName)
            return connection;
    }            
}

private static double ConvertBytesToMB(double bytes) => Math.Round(bytes / 1024 / 1024, 3); 

// Returns the amount of time between each period of network usage for a given granularity
private static TimeSpan GranularityToTimeSpan(DataUsageGranularity granularity)
{
    switch (granularity)
    {
        case DataUsageGranularity.PerMinute:
            return new TimeSpan(0, 1, 0);
        case DataUsageGranularity.PerHour:
            return new TimeSpan(1, 0, 0);
        case DataUsageGranularity.PerDay:
            return new TimeSpan(1, 0, 0, 0);
        default:
            return new TimeSpan(0);
    }
} 

But I don't know my code have some problems or windows 10 build 15063 have some problems because I run my app on two different system and I get same value for all WiFi and Ethernet connections. I always get my network usage with this code but not for a special connection, It's give me my Internet usage however I choose 2 different WiFi.
Is it possible to say me that it is windows problem or my code have some problems?
Thanks.

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
  • This api is only supported on windows phones. Are you trying on a windows phone? – Ken Tucker Aug 20 '17 at 12:10
  • Yes. it's work for windows phone but not windows. where you see that it is only work in Windows mobile not windows 10 and is there any way to get thing that I want. – Amin Borjian Aug 20 '17 at 13:07
  • It is an UWP app. I don't think that is only for windows mobile? – Amin Borjian Aug 20 '17 at 13:59
  • https://learn.microsoft.com/en-us/uwp/api/windows.networking.connectivity.connectionprofile – Ken Tucker Aug 20 '17 at 17:43
  • so is there any other way to get network usage for each connection? – Amin Borjian Aug 20 '17 at 21:45
  • @KenTucker Why did you say this api is only supported om windows phones? The document is only says that **GetAttributedNetworkUsageAsync** method is only supported in a Windows Phone Store app. **GetNetworkUsageAsync** should also works on desktop as it's a Universal API. – Scavenger Aug 21 '17 at 07:54
  • Yes. I didn't notice. GetNetworkUsageAsync is for both but it doesn't work. it work like NetworkInformation.GetInternetConnectionProfile and get usage for Internet Connection profile! – Amin Borjian Aug 21 '17 at 08:05
  • 1
    I think GetAttributedNetworkUsageAsync gets the usage from the phone carrier and that is why it only works on the phone. Of course I cant find any documentation on how it works – Ken Tucker Aug 21 '17 at 10:16
  • I don't know how to solve my problem and there is no other place for ask question. I download network data form windows 10 store and it gives same value for all WiFi in my system and friend system. Is it windows 10 problem? does someone have lower version of windows 10 not 15063 and test it? – Amin Borjian Aug 21 '17 at 11:28
  • No one know how can I ask from Microsoft for problem? – Amin Borjian Aug 22 '17 at 10:19
  • I get an answer finally: https://social.msdn.microsoft.com/Forums/Windowsapps/en-US/e2c741db-7172-4f69-81e9-c2f21f4983d6/uwpc-get-data-usage?forum=wpdevelop – Amin Borjian Aug 22 '17 at 10:50
  • Hi @AminBorjian, is it possible to adapt your code to get data consumption per network type? (wifi, cellular, roaming) – Gold.strike May 03 '18 at 16:41

0 Answers0