有什么方法可以通过网络计算exe/应用程序的带宽(发送和接收的数据包)?已经调查过IP全球地产 http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.aspx,以及其他类。
我希望数据包由单个应用程序发送和接收。我检查过http://netstatagent.com/ http://netstatagent.com/并需要类似的东西。
.Net 中有什么可以帮助我的吗?
我的应用程序连接到网络服务来发送和接收一些图像文件。
一种方法是检索性能计数器 http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx您的应用程序的“.NET CLR 网络/接收的字节”和“.NET CLR 网络/发送的字节”:
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' 在指定类别中不存在)并且您需要插入
<configuration>
<system.net>
<settings>
<performanceCounters enabled="true" />
</settings>
</system.net>
</configuration>
在你的应用程序配置中。
如需完整示例下载网络流量.cs http://pastebin.com/f371375d6 and 网络流量.exe.config http://pastebin.com/f52a1bc2a.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)