博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Access 64-bit HKLM\Software Registry by 32-bit C#.NET Application
阅读量:6544 次
发布时间:2019-06-24

本文共 3508 字,大约阅读时间需要 11 分钟。

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 HKLM\Software, then due to Registry redirection effective path becomes HKLM\Software\Wow6432Node. For example, we are running 64-bit and 32-bit application to read registry keys as HKLM\Software\xyz and HKLM\Software\Wow6432Node\xyz.

So by default, with an input of HKLM\Software, 64-bit application will read HKLM\Software\xyz while because of registry redirection 32-bit application will read HKLM\Software\Wow6432Node\xyz.

In C#, to read 64-bit HKLM\Software 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 = @"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 is 64-bit or not. This function is avialable with .NET Framework 4.0.

转载地址:http://grodo.baihongyu.com/

你可能感兴趣的文章
EF各版本增删查改及执行Sql语句
查看>>
拓扑排序
查看>>
jQGrid API
查看>>
Bzoj1758: [Wc2010]重建计划
查看>>
redis集群部署及踩过的坑
查看>>
j2EE监听器-listener
查看>>
使用pip命令报You are using pip version 9.0.3, however version 18.0 is available pip版本过期.解决方案...
查看>>
(转)LINQ之路
查看>>
Django REST框架--关系和超链接api
查看>>
双击防止网页放大缩小HTML5
查看>>
C#的一些学习方法
查看>>
U3D Invoke() IsInvoking CancelInvoke方法的调用
查看>>
Javascript 如何生成Less和Js的Source map
查看>>
中间有文字的分割线效果
查看>>
<悟道一位IT高管20年的职场心经>笔记
查看>>
volatile和synchronized的区别
查看>>
10.30T2 二分+前缀和(后缀和)
查看>>
vuex视频教程
查看>>
Java 线程 — ThreadLocal
查看>>
安居客爬虫(selenium实现)
查看>>