1. 程式人生 > >通過WMI獲取機器信息

通過WMI獲取機器信息

bytes str except pri 收集 next exception play ict

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獲取機器信息