Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading registry keys using WMI

Tags:

c#

registry

wmi

I am trying to read the registry key by using WMI. I have tried with following code but i am unable to get the registry key value.

Can anyone help me regarding this problem.

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();

}

Note: i want to read the registry keys by using WMI only.

like image 428
Suresh Jaladi Avatar asked Oct 22 '22 08:10

Suresh Jaladi


1 Answers

You must set the value of the hDefKey (hive) parameter and remove the hive from the sSubKeyName parameter.

inParams["hDefKey"] =0x80000002;// HKEY_LOCAL_MACHINE;
inParams["sSubKeyName"] = "SOFTWARE\\Microsoft\\.NETFramework";
like image 186
RRUZ Avatar answered Oct 24 '22 04:10

RRUZ