Udostępnij za pośrednictwem


How to retrieve network connection usage data (HTML)

This topic demonstrates how to access bandwidth usage information on a network connection for a specific period of time using classes in the Windows.Networking.Connectivity namespace.

For general guidance on retrieving connection profiles and accessing information using these objects, see How to retrieve network connection information.

What you need to know

Technologies

Prerequisites

The following example uses JavaScript and are based on the Network information sample. For general help creating a Windows Runtime app using JavaScript, see Create your first Windows Runtime app using JavaScript.

Retrieve internet connection cost data for the past hour

The following example function retrieves the ConnectionProfile for the Internet connection. To retrieve the data we need, the system DateTime (currTime), and a startTime value are passed to the getNetworkUsageAsync method. A NetworkUsage object is returned containing the sent and received values, in bytes, for the requested time period.

For mobile app scenarios, you can add a RoamingStates value to the getNetworkUsageAsync call to scope the requested traffic data to periods of time where a device was roaming.

    var networkInfo = Windows.Networking.Connectivity.NetworkInformation;

    function DisplayLocalDataUsage() {
        var currTime = new Date();

        //Set start Time to 1 hour (3600000ms) before current time
        var startTime = new Date(currTime - 3600000);

        //Get the ConnectionProfile that is currently used to connect to the Internet
        var connectionProfile = networkInfo.getInternetConnectionProfile();
        var LocalUsage = connectionProfile.getNetworkUsageAsync(startTime, currTime);
        var lclString = "Local Data Usage: \n\r";
        lclString += "Bytes Sent: " + LocalUsage.bytesSent + "\n\r";
        lclString += "Bytes Received: " + LocalUsage.bytesReceived + "\n\r";
    }

Note  Follow a similar process to retrieve time-specific cost information for connections not providing internet connectivity. The difference is the initial enumeration of available connection profiles using NetworkInformation.getConnectionProfiles.

 

Behavior differences between Windows Store apps and Windows Phone Store apps

The GetNetworkUsageAsync method has a different behavior on Windows 8.1 and Windows Phone 8.1. When the GetNetworkUsageAsync method is called on Windows Phone, the returned array of NetworkUsage objects will always have the ConnectionDuration property set to 0 since this property is not supported on Windows Phone.

Other

Create your first Windows Runtime app using JavaScript

How to handle exceptions in network apps

How to manage metered network cost constraints

How to manage network connection events and changes in availability

How to retrieve network adapter and locality information

How to retrieve network connection information

Reference

ConnectionProfile.getNetworkUsageAsync

NetworkInformation

NetworkInformation.getConnectionProfiles

Windows.Networking.Connectivity

Samples

Network information sample

Network status background sample