Access 64-bit HKLM\Software Registry by 32-bit C〓NET Appliation

3848 ワード

http://www.codeproject.com/Articles/1003177/Access-bit-HKLM-Software-Registry-by-bit-Csharp-NE
While running 32-bit Windows aplication on a 64-bit windows OS、there is a registry redirection.Here、if 32-bit aplication tries to read a key under HKLM\Software、then due to Registry redirectionwe are running 64-bit and 32-bit aplication to read registry keys as HKLM\Software\xyz and HKLM\Software\Wow 6432 Node\xyz.
So by default、with an input of HKLM\Software、64-bit appication will read HKLM\Software\xyz hile because of registry rection 32-bit appation will read HKLM\Software\
In C菗、to read 64-bit HKLM\Software registry keys、we can use RegistryKey.OpenBaseKey method.This method tars two argments-RegistryHive and RegistryView.Here、seperate registry views aresent for.32 bit.
Here is the sample C芫code to read AppPaths for 32-bit and 64-bit appection s installed on the system:
try
                {
                    string AppPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
                    RegistryKey rkbase = null;
                    rkbase = RegistryKey.OpenBaseKey
                             (Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
                    using (RegistryKey rk = rkbase.OpenSubKey(uninstallKey))
                    {
                        foreach (string skName in rk.GetSubKeyNames())
                        {
                            using (RegistryKey sk = rk.OpenSubKey(skName))
                            {
                                try
                                {

                                    if (sk.GetValue("Path") != null)
                                    {
                                        //add this to required list
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                        }
                    }

                    if (Environment.Is64BitOperatingSystem)
                    {
                        using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(AppPath))
                        {
                            foreach (string skName in rk.GetSubKeyNames())
                            {
                                using (RegistryKey sk = rk.OpenSubKey(skName))
                                {
                                    try
                                    {
                                        if (sk.GetValue("Path") != null)
                                        {
                                            //add this to required list
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
 Here、Environment.Is64BitOperatingSystem is used to check if the current system 64-bit or not.This function is avialable with.NET Framwork 4.0.