次の方法で共有


How to retrieve network connection usage data (XAML)

This topic demonstrates how to access connection bandwidth usage information for a specific period of time from a ConnectionProfile.

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 examples use C# or C++ and are based on the Network information sample. For general help creating a Windows Runtime app using C# or Visual Basic, see Create your first Windows Runtime app using C# or Visual Basic. For general help creating a Windows Runtime app using C++, see Create your first Windows Runtime app using C++.

Knowing what a ConnectionProfile is and how to access the information that it represents is important; for more information see How to retrieve network connection information. For additional code examples, download the Network Information sample.

Retrieve internet connection cost data for the past hour

The following example function first retrieves the ConnectionProfile for the Internet connection. 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 a particular roaming state.

using Windows.Foundation;
using Windows.Networking.Connectivity;

void DisplayLocalDataUsage(object sender, RoutedEventArgs e)
{
    string localDataUsage = string.Empty;
    DateTime CurrTime = DateTime.Now;
    TimeSpan TimeDiff = new TimeSpan(1, 0, 0);
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

    if (InternetConnectionProfile == null)
    {
        LocalUsageData.Text = "Not connected to Internet" + "\n";
    }
    else
    {
        var LocalUsage = InternetConnectionProfile.GetNetworkUsageAsync(CurrTime.Subtract(TimeDiff), CurrTime);

        localDataUsage = "Local Data Usage:\n";
        localDataUsage += " Bytes Sent     : " + LocalUsage.BytesSent + "\n";
        localDataUsage += " Bytes Received : " + LocalUsage.BytesReceived + "\n";
        LocalUsageData.Text = localDataUsage;
    }
}

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 C# or Visual Basic

Create your first Windows Runtime app using C++

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

ConnectionProfile.GetNetworkUsageAsync

NetworkInformation

NetworkUsage

Windows.Networking.Connectivity

Samples

Network information sample

Network status background sample