码迷,mamicode.com
首页 > 其他好文 > 详细

通过WMI获取机器信息

时间:2018-02-23 13:27:09      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:close   byte   counter   img   dsp   play   col   library   sof   

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

通过WMI获取机器信息

标签:close   byte   counter   img   dsp   play   col   library   sof   

原文地址:https://www.cnblogs.com/zhesong/p/wmimc.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!