Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GetNetworkUsageAsync() not working in Windows Store App on Windows 8.1

GetNetworkUsageAsync() not working in Windows Store App on Windows 8.1

Scheduled Pinned Locked Moved C#
csharphelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sharath C V
    wrote on last edited by
    #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.

    P D S 3 Replies Last reply
    0
    • S Sharath C V

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      What is the value in startTime?

      S 1 Reply Last reply
      0
      • S Sharath C V

        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.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • S Sharath C V

          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.

          S Offline
          S Offline
          Sharath C V
          wrote on last edited by
          #4

          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);

          }

          1 Reply Last reply
          0
          • P Pete OHanlon

            What is the value in startTime?

            S Offline
            S Offline
            Sharath C V
            wrote on last edited by
            #5

            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();
            }

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              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

              S Offline
              S Offline
              Sharath C V
              wrote on last edited by
              #6

              I 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 :(

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups