title: Calculating Bandwidth(Sent bytes and receive bytes always return 0)
body: <p>I read this <a href="http://pastebin.com/f371375d6" rel="nofollow">article</a> , I want to get sent bytes and receive bytes for all process, but sent bytes and receive bytes of all process always return 0;
  Here is my code:</p>
  
  <pre><code>namespace Network
  {
      public class NetworkTraffic
      {
          private List&lt;PerformanceCounter&gt; listBytesSentPerformanceCounter;
          private List&lt;PerformanceCounter&gt; listBytesReceivedPerformanceCounter;
          //private PerformanceCounter bytesSentPerformanceCounter;
          //private PerformanceCounter bytesReceivedPerformanceCounter;
  
          public NetworkTraffic()
          {
              listBytesSentPerformanceCounter = new List&lt;PerformanceCounter&gt;();
              listBytesReceivedPerformanceCounter = new List&lt;PerformanceCounter&gt;();
              List&lt;string&gt; listInstanceName = GetInstanceName();
  
              foreach (string str in listInstanceName)
              {
                  PerformanceCounter bytesSentPerformanceCounter = new PerformanceCounter();
                  PerformanceCounter bytesReceivedPerformanceCounter = new PerformanceCounter();
                  bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
                  bytesSentPerformanceCounter.CounterName = "Bytes Sent";
                  bytesSentPerformanceCounter.InstanceName = str;
                  bytesSentPerformanceCounter.ReadOnly = false;
                  listBytesSentPerformanceCounter.Add(bytesSentPerformanceCounter);
  
                  bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
                  bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
                  bytesReceivedPerformanceCounter.InstanceName = str;
                  bytesReceivedPerformanceCounter.ReadOnly = false;
                  listBytesReceivedPerformanceCounter.Add(bytesReceivedPerformanceCounter);
              }
          }
  
          //public float GetBytesSent()
          //{
          //    float bytesSent = bytesSentPerformanceCounter.RawValue;
  
          //    return bytesSent;
          //}
  
          //public float GetBytesReceived()
          //{
          //    float bytesReceived = bytesReceivedPerformanceCounter.RawValue;
  
          //    return bytesReceived;
          //}
  
          private static List&lt;string&gt; GetInstanceName()
          {
              // Used Reflector to find the correct formatting:
              string assemblyName = GetAssemblyName();
              if ((assemblyName == null) || (assemblyName.Length == 0))
              {
                  assemblyName = AppDomain.CurrentDomain.FriendlyName;
              }
              StringBuilder builder = new StringBuilder(assemblyName);
              for (int i = 0; i &lt; builder.Length; i++)
              {
                  switch (builder[i])
                  {
                      case '/':
                      case '\\':
                      case '#':
                          builder[i] = '_';
                          break;
                      case '(':
                          builder[i] = '[';
                          break;
  
                      case ')':
                          builder[i] = ']';
                          break;
                  }
              }
  
              List&lt;string&gt; listInstanceName = new List&lt;string&gt;();
              Process[] listProcess = Process.GetProcesses();
              string InstanceName;
              foreach (Process pro in listProcess)
              {
                  InstanceName = string.Format(CultureInfo.CurrentCulture,
                                       "{0}[{1}]",
                                       builder.ToString(),
                                       pro.Id);
                  listInstanceName.Add(InstanceName);
              }
              return listInstanceName;
          }
          private static string GetAssemblyName()
          {
              string str = null;
              Assembly entryAssembly = Assembly.GetEntryAssembly();
              if (entryAssembly != null)
              {
                  AssemblyName name = entryAssembly.GetName();
                  if (name != null)
                  {
                      str = name.Name;
                  }
              }
              return str;
          }
  
  
          public static void Main()
          {
              NetworkTraffic networkTraffic = new NetworkTraffic();
  
              try
              {
                  while (true)
                  {
                      WebRequest webRequest = WebRequest.Create("http://www.google.com");
                      webRequest.Method = "GET";
  
                      using (WebResponse response = webRequest.GetResponse())
                      using (Stream responseStream = response.GetResponseStream())
                      using (StreamReader reader = new StreamReader(responseStream))
                      {
                      }
                      //Console.WriteLine("Bytes sent: {0}", networkTraffic.GetBytesSent());
                      //Console.WriteLine("Bytes received: {0}", networkTraffic.GetBytesReceived());
                      foreach (PerformanceCounter send in networkTraffic.listBytesSentPerformanceCounter)
                      {
                          Console.WriteLine("Instance name: " + send.InstanceName + " Sent: " + send.RawValue);
                      }
                      Thread.Sleep(1000);
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
              }
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>Anyone can help me resolve this.</p>