有没有办法可以通过网络计算exe /应用程序的带宽(发送和接收的数据包)?已经陷入了IPGlobalProperties,
和其他类....我想要由单个应用程序收到的数据包发送...我已经检查了http://netstatagent.com/ 我需要类似的东西... .net中有什么可以帮助我吗?
我的应用程序连接到Web服务以发送一些图像文件...并且还接收文件...
一种方法是为您的应用程序检索性能计数器 ".NET CLR Networking/Bytes Received"和".NET CLR Networking/Bytes Sent"的值:
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; }
请注意,性能计数器 在第一次使用相关网络库之前不会创建(您将获得InvalidOperation:指定类别中不存在实例'XXX')并且您需要插入
在你的app.config中.
有关完整示例,请下载NetworkTraffic.cs和NetworkTraffic.exe.config.