PerformanceCounter的介绍就不多说了,MSDN上介绍的很详细: https://msdn.microsoft.com/zh-cn/library/system.diagnostics.performancecounter(v=vs.110).aspx

 

 1 //对PerformanceCounter进行封装
 2  public class PerformanceCounterCollect
 3     {
 4         private PerformanceCounter counter = null;
 5         public PerformanceCounterCollect(string categoryName, string counterName, string instanceName)
 6         {
 7             counter = new PerformanceCounter(categoryName, counterName, instanceName, ".");
 8             try
 9             {
10                 Collect();//开启采集
11                 System.Threading.Thread.Sleep(100);
12             }
13             catch { }
14         }
15         public float Collect()
16         {
17             try
18             {
19                 return counter.NextValue();
20             }
21             catch (Exception ex)
22             {
23                 return -1;
24             }
25         }
26     }
View Code

 

收集CpuUtilization:new PerformanceCounterCollect("Processor", "% Processor Time", "_Total")

收集DiskReadSpeed: new PerformanceCounterCollect("PhysicalDisk", "Disk Read Bytes/sec", "_Total")

收集DiskWriteSpeed:new PerformanceCounterCollect("PhysicalDisk", "Disk Write Bytes/sec", "_Total")

收集MemoryAvailable:new PerformanceCounterCollect("Memory", "Available MBytes", "")

收集NetworkSendSpeed这个有一些繁琐,因为机器机器多网卡是很普遍的,要获取Enable的网卡,代码如下:

 

 1  var ethernetInstance = GetEthernetInstance();
 2 
 3   public static string GetEthernetInstance()
 4         {
 5             var result = new PerformanceCounterCategory("Network Interface");
 6             var instanceNames = result.GetInstanceNames();
 7             var ethernetDescriptions = GetEthernetDescriptions();
 8             foreach (var instance in instanceNames)
 9             {
10                 if (ethernetDescriptions.Contains(instance))
11                 {
12                     return instance;
13                 }
14             }
15             return "";
16             //throw new Exception("Not found enable ethernet instance.");
17         }
18 
19  public static List<string> GetEthernetDescriptions()
20         {
21             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
22             ManagementObjectCollection moc = mc.GetInstances();
23             List<string> descriptions = new List<string>();
24             foreach (ManagementObject mo in moc)
25             {
26                 if ((bool)mo["IPEnabled"])
27                 {
28                     descriptions.Add(DesConvertInstance(mo["Description"].ToString()));
29                 }
30             }
31             return descriptions;
32         }
33 static string DesConvertInstance(string description)
34         {
35             foreach (var ch in changeChars)
36             {
37                 description = description.Replace(ch.Key, ch.Value);
38             }
39             return description;
40         }
41 static Dictionary<string, string> changeChars = new Dictionary<string, string>()
42         {
43             {"(", "[" },
44             {")", "]" },
45             {"#", "_" },
46         };
View Code

 

收集NetworkSendSpeed:new PerformanceCounterCollect("Network Interface", "Bytes Sent/sec", ethernetInstance)

收集NetworkReceivedSpeed: new PerformanceCounterCollect("Network Interface", "Bytes Received/sec", ethernetInstance)

WMI还有很多,大家有兴趣可以查看以下网址:

https://msdn.microsoft.com/en-us/library/dn792179(v=vs.85).aspx

posted on 2018-02-23 11:26  Zhe.Song  阅读(1177)  评论(0编辑  收藏  举报