GetNetworkUsageAsync() not working in Windows Store App on Windows 8.1
-
I am trying to get bytes sent and received in a Windows Store App but I am always getting bytes sent/received as zero. Code snippet is shown below. Any help/hint is highly appreciated, thank you :)
ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (connectionProfile.NetworkAdapter.IanaInterfaceType == (int)EnumMediaType.WiFi) { NetworkUsageStates nus = new NetworkUsageStates(); nus.Roaming = TriStates.No; nus.Shared = TriStates.Yes; TimeSpan TimeDiff = new TimeSpan(1, 0, 0); IReadOnlyList<NetworkUsage> listNetworkUsage = await connectionProfile.GetNetworkUsageAsync(startTime.Subtract(TimeDiff), DateTimeOffset.Now, DataUsageGranularity.Total, nus); for (int i = 0; i < listNetworkUsage.Count; i++) { textBoxMediaType.Text = string.Format("Sent: {0}, Received: {1}, Duration: {2}", listNetworkUsage\[i\].BytesSent, listNetworkUsage\[i\].BytesReceived, listNetworkUsage\[i\].ConnectionDuration); }
Note: I am using IE in the background to download some pages to ensure that bytes are sent/received.
-
I am trying to get bytes sent and received in a Windows Store App but I am always getting bytes sent/received as zero. Code snippet is shown below. Any help/hint is highly appreciated, thank you :)
ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (connectionProfile.NetworkAdapter.IanaInterfaceType == (int)EnumMediaType.WiFi) { NetworkUsageStates nus = new NetworkUsageStates(); nus.Roaming = TriStates.No; nus.Shared = TriStates.Yes; TimeSpan TimeDiff = new TimeSpan(1, 0, 0); IReadOnlyList<NetworkUsage> listNetworkUsage = await connectionProfile.GetNetworkUsageAsync(startTime.Subtract(TimeDiff), DateTimeOffset.Now, DataUsageGranularity.Total, nus); for (int i = 0; i < listNetworkUsage.Count; i++) { textBoxMediaType.Text = string.Format("Sent: {0}, Received: {1}, Duration: {2}", listNetworkUsage\[i\].BytesSent, listNetworkUsage\[i\].BytesReceived, listNetworkUsage\[i\].ConnectionDuration); }
Note: I am using IE in the background to download some pages to ensure that bytes are sent/received.
What is the value in startTime?
-
I am trying to get bytes sent and received in a Windows Store App but I am always getting bytes sent/received as zero. Code snippet is shown below. Any help/hint is highly appreciated, thank you :)
ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (connectionProfile.NetworkAdapter.IanaInterfaceType == (int)EnumMediaType.WiFi) { NetworkUsageStates nus = new NetworkUsageStates(); nus.Roaming = TriStates.No; nus.Shared = TriStates.Yes; TimeSpan TimeDiff = new TimeSpan(1, 0, 0); IReadOnlyList<NetworkUsage> listNetworkUsage = await connectionProfile.GetNetworkUsageAsync(startTime.Subtract(TimeDiff), DateTimeOffset.Now, DataUsageGranularity.Total, nus); for (int i = 0; i < listNetworkUsage.Count; i++) { textBoxMediaType.Text = string.Format("Sent: {0}, Received: {1}, Duration: {2}", listNetworkUsage\[i\].BytesSent, listNetworkUsage\[i\].BytesReceived, listNetworkUsage\[i\].ConnectionDuration); }
Note: I am using IE in the background to download some pages to ensure that bytes are sent/received.
You shouldn't be using GetNetworkUsage to determine if a single app is actually sending and receiving data. Since there are a ton of other processes running on the system that can talk on the network there is no way for you to know that the traffic you're seeing in those numbers came from your application. It's returning zeros because the underlying API functions require Windows 8.1 in order to work. RT is apparently not supported. ConnectionProfile.GetNetworkUsageAsync()[^]
A guide to posting questions on CodeProject
How to debug small programs
Dave Kreskowiak -
I am trying to get bytes sent and received in a Windows Store App but I am always getting bytes sent/received as zero. Code snippet is shown below. Any help/hint is highly appreciated, thank you :)
ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (connectionProfile.NetworkAdapter.IanaInterfaceType == (int)EnumMediaType.WiFi) { NetworkUsageStates nus = new NetworkUsageStates(); nus.Roaming = TriStates.No; nus.Shared = TriStates.Yes; TimeSpan TimeDiff = new TimeSpan(1, 0, 0); IReadOnlyList<NetworkUsage> listNetworkUsage = await connectionProfile.GetNetworkUsageAsync(startTime.Subtract(TimeDiff), DateTimeOffset.Now, DataUsageGranularity.Total, nus); for (int i = 0; i < listNetworkUsage.Count; i++) { textBoxMediaType.Text = string.Format("Sent: {0}, Received: {1}, Duration: {2}", listNetworkUsage\[i\].BytesSent, listNetworkUsage\[i\].BytesReceived, listNetworkUsage\[i\].ConnectionDuration); }
Note: I am using IE in the background to download some pages to ensure that bytes are sent/received.
I changed the code as shown below and I am getting values. I just changed the NetworkUsageStates value:
NetworkUsageStates nus = new NetworkUsageStates();
nus.Roaming = TriStates.DoNotCare;
nus.Shared = TriStates.DoNotCare;TimeSpan TimeDiff = new TimeSpan(0, 5, 0);
IReadOnlyList<NetworkUsage> listNetworkUsage = await connectionProfile.GetNetworkUsageAsync(startTime.Subtract(TimeDiff), DateTimeOffset.Now, DataUsageGranularity.Total, nus);
for (int i = 0; i < listNetworkUsage.Count; i++)
{
textBoxMediaType.Text = string.Format("Sent: {0}, Received: {1}, Duration: {2}", listNetworkUsage[i].BytesSent, listNetworkUsage[i].BytesReceived, listNetworkUsage[i].ConnectionDuration);}
-
What is the value in startTime?
Value of startTime is 5 minutes lesser than app start time which gets set in MainPage() ctor. Code shown below:
public MainPage()
{
startTime = DateTimeOffset.Now;
this.InitializeComponent();
} -
You shouldn't be using GetNetworkUsage to determine if a single app is actually sending and receiving data. Since there are a ton of other processes running on the system that can talk on the network there is no way for you to know that the traffic you're seeing in those numbers came from your application. It's returning zeros because the underlying API functions require Windows 8.1 in order to work. RT is apparently not supported. ConnectionProfile.GetNetworkUsageAsync()[^]
A guide to posting questions on CodeProject
How to debug small programs
Dave KreskowiakI am actually on Windows 8.1 (sorry for the confusing subject line, I should have written Windows Store App instead). When I changed NetworkUsageStates' Roaming and Shared to DoNotCare I started getting values. Still evaluating to see if the values that I get are correct. I want the complete bytes sent and received, so no issues for me.
NetworkUsageStates nus = new NetworkUsageStates();
nus.Roaming = TriStates.DoNotCare;
nus.Shared = TriStates.DoNotCare;Note: Thanks for the note that this would not work in Windows RT. This is not good news for me :(