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/archive/2010/08/17/WMISys.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值