C#上网流量统计 昨天,听到领导对java的同事说,做一个流量统计功能,看你的程序是不是把带宽占完了,占完了好申请更多的带宽,现在什么360,金山,QQ管家各种软件都在做流量统计,就想自己能不能也实现一个呢/? 网上搜索了一下,没找到C#做流量统计的资料,我想C++应该可以三,继续搜索,意外发现windows有自己的流量统计功能,只需要去取出来就好了。 O~LA~这么简单的事情C#应该有函数的,MSDN打开,网络通信相关的命名空间看看去,O~找到了,原来确实是很简单的,贴出来大家看看: using System.Net.NetworkInformation; IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); Console.WriteLine("Interface information for {0}.{1} ", computerProperties.HostName, computerProperties.DomainName); if (nics == null || nics.Length < 1) { Console.WriteLine(" No network interfaces found."); return; } Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length); foreach (NetworkInterface adapter in nics) { IPv4InterfaceStatistics ipv4Statistics = adapter.GetIPv4Statistics(); Console.WriteLine("send :{0}MB", ipv4Statistics.BytesSent / 1024 / 1024); Console.WriteLine("recieve :{0}MB", ipv4Statistics.BytesReceived / 1024 / 1024); }