Read Remote Registry Value in C# using WMI and Remote Registry

Description

In this article, I am going write C# code to Read Registry Value from Remote Machine using WMI Service and Remote Registry service.

Summary

Read Registry Value from Remote Machine via WMI Service using C#

You can read Registry Value and Key in C# by using WMI class StdRegProv instead of using .NET class RegistryKey. You can use below C# function to get list of installed programs from remote machine by reading Registry Value via 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;
    }

Read Registry Value from Remote Computer in C# using Remote Registry Service

Noramly, you can Read Registry Value in C# by using built-in C# function RegistryKey, But when you read registry from remote machine by using this class, you need to start the Windows Service “Remote Regitry” in target machine(remote).

Read Remote Registry Value in C# using WMI and Remote Registry

By default, Remote Registry service will be enabled and started automatically in Server editions (i.e Windows Server 2008 R2) but in Client editions, you have to enable manually.

You can read Registry Value and Key from Remote Machine by using following function.

private static List<string> ReadRegistryFromRemoteMachine(string machineName)
    {
        List<string> programs = new List<string>();

        string softwareRegLoc = @"SoftwareMicrosoftWindowsCurrentVersionUninstall";
        // Open Remote Machine Registry Key 
        RegistryKey remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName);

        RegistryKey regKey = remoteKey.OpenSubKey(softwareRegLoc);

        foreach (string subKeyName in regKey.GetSubKeyNames())
        {
            // Open Registry Sub Key
            RegistryKey subKey = regKey.OpenSubKey(subKeyName);

            // Read Value from Registry Sub Key
            string softwareName = (string)subKey.GetValue("DisplayName");

            if (!string.IsNullOrEmpty(softwareName))
            {
                programs.Add(softwareName);
            }
        }

        return programs;
    }

Advertisement

Leave a Comment