Thursday, November 8, 2012

Read Registry value from 32 and 64 Bit machine using C#.


Read Registry value from 32 and 64 Bit machine using C#.

Hello,

Below code block use to read registry value from 32 or 64 bit machine.

First we need to check whether we have 32 bit or 64 bit machine.

bool Is64bit = !string.IsNullOrEmpty(
                         Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"));

Is64bit variable return true value if machine is 64 bit.

Now according to 64 or 32 bit machine, we will retrieve registry value.





            RegistryKey rkCurrentUser = Registry.LocalMachine;

            if (Is64bit == true)
            {
                string KeyPath64 = @"SOFTWARE\Wow6432Node\MySoftware\MyKey";
                using (var key = rkCurrentUser.OpenSubKey(KeyPath64))
                {
                    var LastSetValue = (string)key.GetValue("LastSet");
                }
            }
            else
            {
                const string KeyPath32 = @"SOFTWARE\\ MySoftware\\ MyKey ";
                using (var key = rkCurrentUser.OpenSubKey(KeyPath32))
                {
                    var LastSetValue = (string)key.GetValue("LastSet");
                }
            }

Enjoy !!!!!

No comments:

Post a Comment