zoukankan      html  css  js  c++  java
  • Access 64-bit HKLMSoftware Registry by 32-bit C#.NET Application

    http://www.codeproject.com/Articles/1003177/Access-bit-HKLM-Software-Registry-by-bit-Csharp-NE

    While running 32-bit Windows application on a 64-bit windows OS, there is a registry redirection. Here, if 32-bit application tries to read a key under HKLMSoftware, then due to Registry redirection effective path becomes HKLMSoftwareWow6432Node. For example, we are running 64-bit and 32-bit application to read registry keys as HKLMSoftwarexyz and HKLMSoftwareWow6432Nodexyz.

    So by default, with an input of HKLMSoftware, 64-bit application will read HKLMSoftwarexyz while because of registry redirection 32-bit application will read HKLMSoftwareWow6432Nodexyz.

    In C#, to read 64-bit HKLMSoftware registry keys, we can use RegistryKey.OpenBaseKey method. This method takes two arguments- RegistryHive and RegistryView. Here, seperate registry views are present for 32-bit and 64-bit.

    Here is the sample C# code to read AppPaths for 32-bit and 64-bit applications installed on the system:

    try
                    {
                        string AppPath = @"SOFTWAREMicrosoftWindowsCurrentVersionApp 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 is 64-bit or not. This function is avialable with .NET Framework 4.0.

  • 相关阅读:
    Java程序执行超时——Future接口介绍
    JENKINS 打包发布脚本
    获取servletContext springMvc获取servletContext
    14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制:
    14.18.1 The InnoDB Recovery Process InnoDB 恢复进程:
    perl 获取文件内容里第一个AAA和最后一个AAA
    14.18 InnoDB Backup and Recovery 备份和恢复:
    职业素养与职业声誉——北漂18年(62)
    Openstack组件实现原理 — Nova 体系结构
    Openstack组件实现原理 — Nova 体系结构
  • 原文地址:https://www.cnblogs.com/zeroone/p/4604164.html
Copyright © 2011-2022 走看看