Description
In this article, I am going write C# code snippets Read Registry Value using WMI in C# and C# code to Read Registry Key from Remote Machine or Remote Computer using WMI.
Summary
Read Registry Value using WMI in C#
You can read Registry Value and Registry Key in C# by using WMI class StdRegProv. Here, I have written sample function to read installed programs by reading Control Panel Registry Entries using WMI in C#.
private static List<string> ReadRegistryusingWMI()
{
List<string> programs = new List<string>();
ManagementScope scope = new ManagementScope("\.rootCIMV2");
scope.Connect();
string softwareRegLoc = @"SoftwareMicrosoftWindowsCurrentVersionUninstall";
ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
ManagementBaseObject inParams = registry.GetMethodParameters("EnumKey");
inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
inParams["sSubKeyName"] = softwareRegLoc;
// Read Registry Key Names
ManagementBaseObject outParams = registry.InvokeMethod("EnumKey", inParams, null);
string[] programGuids = outParams["sNames"] as string[];
foreach (string subKeyName in programGuids)
{
inParams = registry.GetMethodParameters("GetStringValue");
inParams["sSubKeyName"] = softwareRegLoc + @"" + subKeyName;
inParams["sValueName"] = "DisplayName";
// Read Registry Value
outParams = registry.InvokeMethod("GetStringValue", inParams, null);
if (outParams.Properties["sValue"].Value != null)
{
string softwareName = outParams.Properties["sValue"].Value.ToString();
programs.Add(softwareName);
}
}
return programs;
}
//HKEY_CLASSES_ROOT (2147483648 (0x80000000))
//HKEY_CURRENT_USER (2147483649 (0x80000001))
//HKEY_LOCAL_MACHINE (2147483650 (0x80000002))
//HKEY_USERS (2147483651 (0x80000003))
//HKEY_CURRENT_CONFIG (2147483653 (0x80000005))
Read Registry Value from Remote Machine via WMI using C#
You can read Registry Value and Registry Key in C# by using WMI class StdRegProv. Here, I have written sample function to read installed programs in Remote Machine by reading Control Panel Registry Entries using WMI in C#.
private static List<string> ReadRemoteRegistryusingWMI(string machineName)
{
List<string> programs = new List<string>();
ConnectionOptions connectionOptions = new ConnectionOptions();
//connectionOptions.Username = @"DomainAdministrator";
//connectionOptions.Password = "password";
//connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope("\" + machineName + "rootCIMV2", connectionOptions);
scope.Connect();
string softwareRegLoc = @"SoftwareMicrosoftWindowsCurrentVersionUninstall";
ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
ManagementBaseObject inParams = registry.GetMethodParameters("EnumKey");
inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
inParams["sSubKeyName"] = softwareRegLoc;
// Read Registry Key Names
ManagementBaseObject outParams = registry.InvokeMethod("EnumKey", inParams, null);
string[] programGuids = outParams["sNames"] as string[];
foreach (string subKeyName in programGuids)
{
inParams = registry.GetMethodParameters("GetStringValue");
inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
inParams["sSubKeyName"] = softwareRegLoc + @"" + subKeyName;
inParams["sValueName"] = "DisplayName";
// Read Registry Value
outParams = registry.InvokeMethod("GetStringValue", inParams, null);
if (outParams.Properties["sValue"].Value != null)
{
string softwareName = outParams.Properties["sValue"].Value.ToString();
programs.Add(softwareName);
}
}
return programs;
}
Advertisement
Thanks for your source snippets. It works 😉
I get error for
"SOFTWAREMicrosoftMicrosoft SQL ServerInstance NamesSQL"
do you have any suggestion?
Thanks
Great! how to differentiate between 64bits view and 32 bits view
I think. below posts will help you : https://www.morgantechspace.com/2016/02/how-to-read-64-bit-registry-from-32-bit-program.html
https://www.morgantechspace.com/2016/02/force-wmi-to-read-32-bit-registry-hive-in-64-bit-machine.html
ConnectionOptions and ManagementScope are missing do I need to include any libraries?
Yes, you need to add the reference for System.Management.dll : https://docs.microsoft.com/en-us/dotnet/api/system.management
thank you!. do you think its possible just to list the key and values from SoftwareMicrosoftWindowsCurrentVersionTESTSOFTWARE for example? in that patch I have three values: name, version and date, I haven't been able to figure how to enumerate just the keys in the path. thanks for your time.