Read Registry Value using WMI in C#

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

7 thoughts on “Read Registry Value using WMI in C#”

Leave a Comment