2013-06-04 92 views
5

我正尝试使用WMI读取注册表项。我已经尝试使用以下代码,但我无法获取注册表项值。使用WMI读取注册表项

任何人都可以帮我解决这个问题。

ConnectionOptions oConn = new ConnectionOptions(); 
System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\" +hostname + @"\root\cimv2", oConn); 

scope.Connect(); 
ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null); 
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue"); 
inParams["sSubKeyName"] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETFramework"; 
inParams["sValueName"] = "InstallRoot"; 


ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null); 

if (outParams.Properties["sValue"].Value != null) 
{ 
output = outParams.Properties["sValue"].Value.ToString(); 

} 

注意:我想通过仅使用WMI读取注册表项。

+1

你会得到什么结果?异常/错误,空值或意外值? – Marcus

回答

3

您必须设置hDefKey(配置单元)参数的值,并从sSubKeyName参数中除去配置单元。

inParams["hDefKey"] =0x80000002;// HKEY_LOCAL_MACHINE; 
inParams["sSubKeyName"] = "SOFTWARE\\Microsoft\\.NETFramework";