Thursday, 3 April 2014

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("\\\\.\\root\\CIMV2");
        scope.Connect();

        string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";

        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 = @"Domain\Administrator";
        //connectionOptions.Password = "password";
        //connectionOptions.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope("\\\\" + machineName + "\\root\\CIMV2", connectionOptions);
        scope.Connect();

        string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";

        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;
    }

Advertisements
Advertisements

4 comments:

  1. Thanks for your source snippets. It works ;-)

    ReplyDelete
  2. I get error for
    "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"
    do you have any suggestion?
    Thanks

    ReplyDelete
  3. Great! how to differentiate between 64bits view and 32 bits view

    ReplyDelete
    Replies
    1. I think. below posts will help you : http://www.morgantechspace.com/2016/02/how-to-read-64-bit-registry-from-32-bit-program.html

      http://www.morgantechspace.com/2016/02/force-wmi-to-read-32-bit-registry-hive-in-64-bit-machine.html

      Delete