网络流量监控程序

先传一个图片上来吧。


实现方法 :

这个程序的实现的方法主要使用WMI的方式,动态的获取WMI中的资料。并计算得出结果显示到界面中。


主要使用的WMI的类是:Win32_PerfRawData_Tcpip_NetworkInterface,Win32_NetworkAdapter

先通过Win32_NetworkAdapter 获取到计算机中的所有的网络适配器的名称。然后选择不同的名称后,动态的获取底层的资料。

获取计算机上适配器的方法如下:

IList<Win32_NetworkAdapter> NetwordItems = null;
NetworkAdapter NA = null;
NA = new NetworkAdapter(ComputerName);
NetwordItems = NA.SelectAll();

foreach (Win32_NetworkAdapter item in NetwordItems)
{
string s = item.AdapterType;
if (s != null)
{
if (item.ServiceName == "PptpMiniport")
{
continue;
}
UpNetItems(item.NetConnectionID,item.Name);
}
}

计算网速的主要算法如下:

pni.m_where = " where Name = '" + result + "'";
NetList.Clear();
NetList = pni.SelectAll();
Win32_PerfRawData_Tcpip_NetworkInterface item = NetList[0] as Win32_PerfRawData_Tcpip_NetworkInterface;

float CounterValue1 = (float)item.BytesReceivedPersec;
float upv1 = (float)item.BytesSentPersec;
float TimeValue1 = (float)item.Timestamp_PerfTime;
float TimeBase = (float)item.Frequency_PerfTime;
System.Threading.Thread.Sleep(500);

NetList.Clear();
NetList = pni.SelectAll();
item = NetList[0] as Win32_PerfRawData_Tcpip_NetworkInterface;

float CounterValue2 = (float)item.BytesReceivedPersec;
float upv2 = (float)item.BytesSentPersec;

float TimeValue2 = (float)item.Timestamp_PerfTime;

if (TimeValue2 - TimeValue1 == 0)
{ }
else
{
float downs = (CounterValue2 - CounterValue1) / ((TimeValue2 - TimeValue1) / TimeBase) / 1024;
float up = (upv2 - upv1) / ((TimeValue2 - TimeValue1) / TimeBase) / 1024;
UpdataValue(downs);
UpdataValue2(up);


}

这个程序中使用到一个EasyWMI的dll,这个dll中包括了所有WMI的定义。这个可以到codeProject 搜索一下 下载。

WMI的功能很强大,几乎所有的计算机上的信息都可以通过这个查到。很方便开发一些性能监控等程式。以后有时间准备写一个使用WMI实现计算机监控管理的程序来。

不过最近一直忙着用vc写的一个带UI的exe 的Com的程序。最近一段时间应该是没有什么时间了。

很晚了, 睡觉啦




对应的控件 :http://www.codeproject.com/Articles/17564/Simple-Performance-Chart

原文地址:https://www.cnblogs.com/zhucl1006/p/1006332.html