Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
64 views
in Technique[技术] by (71.8m points)

c# - Calculating Bandwidth

Is there any way i can calculate bandwidth (packets sent and received) by an exe/application via net? have looked into IPGlobalProperties, and other classes.

I want packets sent and received by a single application. I have checked http://netstatagent.com/ and need something similar.

Is there anything in .Net which can help me?

My app connects to web service to send and receive some image files.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way is to retrieve the value of the performance counters ".NET CLR Networking/Bytes Received" and ".NET CLR Networking/Bytes Sent" for your application:

PerformanceCounter bytesSentPerformanceCounter= new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking";
bytesSentPerformanceCounter.CounterName = "Bytes Sent";
bytesSentPerformanceCounter.InstanceName = GetInstanceName();
bytesSentPerformanceCounter.ReadOnly = true;

float bytesSent = bytesSentPerformanceCounter.NextValue();

//....

private static string 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 < builder.Length; i++)
  {
    switch (builder[i])
    {
      case '/':
      case '\':
      case '#':
        builder[i] = '_';
        break;
      case '(':
        builder[i] = '[';
        break;

      case ')':
        builder[i] = ']';
        break;
    }
  }
  return string.Format(CultureInfo.CurrentCulture, 
                       "{0}[{1}]", 
                       builder.ToString(), 
                       Process.GetCurrentProcess().Id);
}

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

Note that the performance-counters aren't created until the first time you use the relevant network libraries (you will get InvalidOperation : Instance 'XXX' does not exist in the specified Category) and that you need to insert

<configuration>
  <system.net>
    <settings>
      <performanceCounters enabled="true" />
    </settings>
  </system.net>
</configuration>

in your app.config.

For a full sample download NetworkTraffic.cs and NetworkTraffic.exe.config.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...