使用WMI读取注册表键

5

我正在尝试使用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
1个回答

4

您必须设置 hDefKey (注册表项) 参数的值,并从 sSubKeyName 参数中删除该注册表项。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接