先上代码及实例
RegistryKey rsg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Macromedia\FlashPaper Printer\2\Installation\", false);
为什么返回值是NULL
L
原因其实是在64位电脑上跑32位程序,以上代码读取为空值,32位电脑读注册表使用OpenSubKey是正确的
解决方法:

I will show you how to get connecting string stored in registry. The code which I will show in this blog will work both on 32 and 64 bit machines. Let's say your connectingstring key name is "MyAppConnectionStringKey" and stored at following path "SOFTWARE\MyApp\Settings" with following value "Initial Catalog=mydb;Server=MY-PC;User ID=sa;Password=test"

Goal:
To get the "MyAppConnectionStringKey" value via code.

Solution:
First of all add the following "RegistryHelpers" class in your solution. I have added few methods in this class to get registrykey and value.

using System;
using Microsoft.Win32; namespace TestConsole
{
public class RegistryHelpers
{ public static RegistryKey GetRegistryKey()
{
return GetRegistryKey(null);
} public static RegistryKey GetRegistryKey(string keyPath)
{
RegistryKey localMachineRegistry
= RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem
? RegistryView.Registry64
: RegistryView.Registry32); return string.IsNullOrEmpty(keyPath)
? localMachineRegistry
: localMachineRegistry.OpenSubKey(keyPath);
} public static object GetRegistryValue(string keyPath,string keyName)
{
RegistryKey registry = GetRegistryKey(keyPath);
return registry.GetValue(keyName);
}
}
}

Let's test this code with a console program to fetch the value of key:

using System;

namespace TestConsole
{
internal class Program
{
private static void Main(string[] args)
{
string keyPath = @"SOFTWARE\MyApp\Settings";
string keyName = "MyAppConnectionStringKey"; object connectionString = RegistryHelpers.GetRegistryValue(keyPath, keyName); Console.WriteLine(connectionString);
Console.ReadLine();
}
}
}

Output:

 
 

注册表键值明明存在OpenSubKey始终返回null,解决方案的更多相关文章

  1. 使用PowerShell读、写、删除注册表键值

    访问注册表键值 在PowerShell中,用户可以通过类似于HKCU:(作为HKEY_CURRENT_USER)和HKLM:(代表HKEY_LOCAL_MATCHINE)的虚拟驱动器访问注册表键值. ...

  2. 通过程序修改注册表键值来达到修改IE配置参数的目的

    通过程序修改注册表键值来达到修改IE配置参数的目的 使用IE访问应用程序或网页时经常需要设置一些选项(工具-Internet 选项),比如为了避免缓存网页,把工具-Internet选项-常规选项卡-I ...

  3. 使用Windows命令行reg控制注册表键值

    使用Windows命令行reg控制注册表键值 引言 熟悉Windows操作系统的朋友可能都知道,Windows操作系统下的注册表相当于系统的数据库 ,部分软件将自己的配置信息都放在注册表里面,而注册表 ...

  4. Ubuntu - Dconf 注册表键值修改参考表

    gsettings reset org.gnome.desktop.wm.preferences theme默认gnomegsettings set org.gnome.desktop.interfa ...

  5. NSIS:检查某注册表键是否存在

    原文NSIS:检查某注册表键是否存在 ;定义注册表主键!define HKEY_CLASSES_ROOT           0x80000000!define HKEY_CURRENT_USER   ...

  6. 如何查询注册表的值及 Powershell 应用

    利用 c:\windows\system32\reg.exe 的 query 参数即可. reg.exe 的参数如下: C:\windows\system32> reg.exe /?REG Op ...

  7. Android fragment-findFragmentByTag 始终返回 null

    我曾四处看看,在我的案子中找到几个与类似的主题,但没有帮助的问题.我想访问现有活动片段使用getSupportFragmentManager().findFragmentByTag(TAG),但它始终 ...

  8. 如何通过.reg文件来修改注册表键和子键以及键值

    无废话, 直接上例子, 自己运行一下便知. 然后根据自己需要改改就可以随便用了.   添加key, subkey, 和设置键值的例子. ==================== Windows Reg ...

  9. SQL Server外键关系是强制约束,外键值也可以是空(NULL)

    在SQL Server中,实际上外键值可不可以为空(NULL),和外键关系是不是强制约束无关. 我们先在SQL Server数据库中建立两张表People和Car,一个People可以有多个Car,所 ...

随机推荐

  1. 02-MySQL的安装和配置

    1. 软件和环境 注:安装MySQL数据库的操作系统必须保证有.NET环境和VC运行库的支持.    下载地址:百度云网盘链接 2. MySQL服务器安装详细步骤 (1). 选择安装类型 这里我们选择 ...

  2. 无线路由器的web漏洞

    1.XSS漏洞 2.CSRF漏洞 这样的攻击方式具体可以由多种形式实现,除了建立欺诈服务器外,还可以使用超链接陷阱.所谓的超链接陷阱,就是指伪造一个可信的链接或者地址,但其实际地址中包含了修改无线路由 ...

  3. A - A Secret (扩展kmp)

    题目链接:https://cn.vjudge.net/contest/283743#problem/A 题目大意:给你字符串s1和s2,然后问你s2的每一个后缀在s1中出现的次数之和(可重叠). 具体 ...

  4. mysql 案例 ~ pt-archiver 归档工具的使用

    一 简介:今天咱们来聊聊pt-archiver的使用 二 相关参数 相关参数1   --statistics 结束的时候给出统计信息:开始的时间点,结束的时间点,查询的行数,归档的行数,删除的行数,以 ...

  5. mysql 查询优化 ~explain解读之type的解读

    一 简介:今天咱们来聊聊explain中type的相关解读 二 类型: system: 表中只有一条数据. 这个类型是特殊的 const 类型.  const: 针对主键或唯一索引的等值查询扫描, 最 ...

  6. Delpher 必记-delphi环境安装

    //Delpher 必记 环境: 安装包: 1.所有第三方控件包放在没有中文名的路径:如F:\DComp 安装包放在对应的版本的文件夹里面(实际中没有分类),然后看安装包的引用路径和输出路径,都要设定 ...

  7. 让webstrom更好用的设置

    一.让webstrom在编辑vue项目时更快 1.在webstrom的项目管理树中,找到node_modules文件夹,在文件夹上点右键,在出来的右键菜单中选择“Mark Directory as” ...

  8. Linux组管理和权限管理

    ⒈Linux组基本介绍 1)在Linux中的每个用户必须属于一个组,不能独立于组外. 2)Linux中每个文件都有所有者.所在组.其它组的概念 ①所有者 一般(默认)为文件的创建者,谁创建了该文件,就 ...

  9. 【逆向工具】IDA使用1-VS2015版本debug查找Main函数,加载符号文件

    IDA 常见操作 空格,切换反汇编视图 选择CALL或是跳转 进入函数内部或是跳转处 返回键 ESC daq.exe 分析32位程序 ,生成的IDA数据库文件是 .idb Idap64.exe 分析6 ...

  10. mono修改配置

    当前mono安装目录为:/home/mono,安装成功后修改配置需进入这个路径: cd /home/mono 1.修改TcpBinaryFrameManager.cs文件 cd /home/mono/ ...