Thursday, 3 April 2014

Read and Write Registry Value using C#

Description:

In this article, I am going write C# code to Read and Write Registry Key and Registry Value, C# code to Read Registry Key from Remote machine or Remote computer and Read Registry Value using WMI in Local and Remote machine.

Summary:


Read Registry Value and Registry Key in C#

You can Read Registry Value in C# by using built-in C# function RegistryKey. Here, I have written simple function to list installed software programs in local machine by reading Control Panel Registry entries
    private static List<string> GetInstalledSoftwares()
    {
        List<string> programs = new List<string>();

        string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
        // Open Registry Key in RegistryHive.LocalMachine
        RegistryKey regKey = Registry.LocalMachine.OpenSubKey(softwareRegLoc, false);
            
        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;
    }

Write Registry Value and Registry Key using C#

You can create Registry Key and write Registry Value in C# by using built-in C# function RegistryKey. Here, I have written simple function to create registry sub key 'MorganApps' and write registry values in this sub key.

private static void WriteRegistry()
    {
        string softwareRegKey = @"Software";
            
        // Set second argument as 'True' to give write access in this key
        RegistryKey regKey = Registry.LocalMachine.OpenSubKey(softwareRegKey, true);

        RegistryKey appKey = regKey.CreateSubKey("MorganApps");

        appKey.SetValue("DisplayName", (object)"WriteRegApp", RegistryValueKind.String);
        appKey.SetValue("Publisher", (object)"Morgan Tech Space", RegistryValueKind.String);
        appKey.SetValue("Version", (object)"v1.0", RegistryValueKind.String);
    }


After executing this C# code, you can see the Registry Key 'MorganApps' like shown in below image

Read Registry Value using C# from Remote Machine


Read Registry Value from Remote Machine in C#

You can read Registry Value and Key from Remote Machine in C# by using following function.
private static List<string> ReadRegistryFromRemoteMachine(string machineName)
    {
        List<string> programs = new List<string>();

        string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
        // 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;
    }

Read Registry Value via WMI using C#

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

Thanks,
Morgan
Software Developer

Advertisements
Advertisements

No comments:

Post a Comment