开发网络监控器

尔凡巴蒂

新成员
已加入
2014年11月29日
留言内容
3
编程经验
Beginner
我想开发类似于Windows资源监视器的应用程序(仅网络选项卡)
我是新来的,请指导我如何实现这一目标
 

狂暴风暴

知名会员
已加入
2014年8月6日
留言内容
85
地点
南非鲁德普特
编程经验
10+

尔凡巴蒂

新成员
已加入
2014年11月29日
留言内容
3
编程经验
Beginner
班级计划
{


静态void Main(string [] args)
{




foreach(Process.GetProcesses()中的过程pss)
{

NetworkTraffic trafficMonitor =新的NetworkTraffic(pss.Id);
字符串totalBandwidthConsumption;

字符串lastAmountOfBytesReceived;
float currentAmountOfBytesReceived = trafficMonitor.GetBytesReceived();
float currentAmountOfBytesSent = trafficMonitor.GetBytesSent();


totalBandwidthConsumption = string.Format("总带宽消耗:{0} kb",(currentAmountOfBytesReceived + currentAmountOfBytesSent)/ 1024);

Console.WriteLine("Process Name:+"+ pss.ProcessName.ToString());
Console.WriteLine("Bytes Sent:+"+ currentAmountOfBytesSent);
Console.WriteLine("Bytes Receive:+"+ currentAmountOfBytesReceived);
Console.WriteLine("Total Bytes:+"+ totalBandwidthConsumption);
}
}

}


公共类NetworkTraffic
{
专用PerformanceCounter bytesSentPerformanceCounter;
私有PerformanceCounter bytesReceivedPerformanceCounter;
private int pid;
私人布尔柜台


公共NetworkTraffic(int processID)
{
pid = processID;
TryToInitializeCounters();
}


私有void TryToInitializeCounters()
{
如果(!countersInitialized)
{
PerformanceCounterCategory类别=新的PerformanceCounterCategory(".NET CLR网络4.0.0.0");


var instanceNames = category.GetInstanceNames()。Where(i =>i.Contains(string.Format("p{0}", pid)));


如果(instanceNames.Any())
{
bytesSentPerformanceCounter = new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName =".NET CLR网络4.0.0.0";
bytesSentPerformanceCounter.CounterName ="Bytes Sent";
bytesSentPerformanceCounter.InstanceName = instanceNames.First();
bytesSentPerformanceCounter.ReadOnly = true;


bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName =".NET CLR网络4.0.0.0";
bytesReceivedPerformanceCounter.CounterName ="Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = instanceNames.First();
bytesReceivedPerformanceCounter.ReadOnly = true;


countersInitialized = true;
}
}
}


公共浮动GetBytesSent()
{
float bytesSent = 0;


try
{
TryToInitializeCounters();
bytesSent = bytesSentPerformanceCounter.RawValue;
}
catch { }


return bytesSent;
}


公共浮动GetBytesReceived()
{
float bytesSent = 0;


try
{
TryToInitializeCounters();
bytesSent = bytesReceivedPerformanceCounter.RawValue;
}
catch { }


return bytesSent;
}
}

我正在获取进程名称,但未发送和接收数据包
IE
流程名称:+smss
发送的字节数:+0
接收字节数:+0
总字节数:+Total Bandwidth Consumption: 0 kb
流程名称:+explorer
发送的字节数:+0
接收字节数:+0
总字节数:+Total Bandwidth Consumption: 0 kb
流程名称:+System
发送的字节数:+0
接收字节数:+0

有人有什么主意吗?????请指导
 
最佳 底部