WMI访问注册表读取系统信息

在一台host上创建了n台机子,想读取每台机子的系统信息(版本信息)。在host 上创建一个WCF服务:
通过WMI的root\DEFAULT:StdRegProv访问注册表;通过调用EnumKey函数,
从SOFTWARE\Microsoft\Windows NT\CurrentVersion 下读出系统的版本信息。
从SYSTEM\CurrentControlSet\Control\Session Manager\Environment 写读出CPU的位数。
然后生成新的XML。
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Runtime.Serialization;
5  using System.ServiceModel;
6  using System.Text;
7 using Microsoft.Win32;
8 using System.Xml.Linq;
9 using System.Management;
10 namespace WcfServices
11 {
12 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
13 public class Service1 : IService1
14 {
15
16 #region IService1 Members
17 public System.Xml.Linq.XElement GetSysInfoXML(string machineName)
18 {
19 XElement xmlMain = null;
20 Dictionary<string, string> sysInfo = GetSynInfo(machineName);
21 if (null != sysInfo)
22 {
23 xmlMain = new XElement("AndVerifier");
24 foreach (var item in sysInfo)
25 {
26 XAttribute attr1 = new XAttribute("ExpectedValues", item.Value);
27 XAttribute attr2 = new XAttribute("Root", "LocalMachine");
28 XAttribute attr3 = new XAttribute("Key", item.Key);
29 XElement element = new XElement("RegistryValueVerifier", attr1, attr2, attr3);
30 xmlMain.Add(element);
31 }
32 }
33 return xmlMain;
34 }
35 public Dictionary<string, string> GetSynInfo(string machineName)
36 {
37 // string machineName = "B1-Site";
38 Dictionary<string, string> sysInfo = new Dictionary<string, string>();
39 string regMain = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
40 string archreg = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
41 string[] subKey = { "CSDVersion", "CurrentBuildNumber", "CurrentVersion", "ProductName"};
42 ManagementClass mClass = new ManagementClass(@"\\" + machineName + @"\root\DEFAULT:StdRegProv");
43 mClass.Scope.Options.Password = "User@123";
44 mClass.Scope.Options.Username = "Administrator";
45 object[] method_args = new object[] { 0x80000002, regMain, null };
46 object result = mClass.InvokeMethod("EnumKey", method_args);
47 foreach (var item in subKey)
48 {
49 ManagementBaseObject inParams = mClass.GetMethodParameters("GetStringValue");
50 inParams["hDefKey"] = 0x80000002;
51 inParams["sSubKeyName"] = regMain;
52 inParams["sValueName"] = item;
53 ManagementBaseObject curRes = mClass.InvokeMethod("GetStringValue", inParams, null);
54 if (curRes["sValue"] != null)
55 {
56 string keyValue = curRes["sValue"].ToString();
57 string keyString = regMain + "\\" + item;
58 sysInfo.Add(keyString, keyValue);
59 }
60 }
61 ManagementBaseObject ainParams = mClass.GetMethodParameters("GetStringValue");
62 ainParams["hDefKey"] = 0x80000002;
63 ainParams["sSubKeyName"] = archreg;
64 ainParams["sValueName"] = "PROCESSOR_ARCHITECTURE";
65 ManagementBaseObject curReg = mClass.InvokeMethod("GetStringValue", ainParams, null);
66 if (curReg["sValue"] != null)
67 {
68 string keyValue = curReg["sValue"].ToString();
69 string keyString = archreg + "\\" + "PROCESSOR_ARCHITECTURE";
70 sysInfo.Add(keyString, keyValue);
71 }
72
73 return sysInfo;
74 }
75 #endregion
76 }
77 }
原文地址:https://www.cnblogs.com/jimson/p/WMISys.html