注册表键值明明存在OpenSubKey始终返回null,解决方案
为什么返回值是NULL
LI 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,解决方案的更多相关文章
- 使用PowerShell读、写、删除注册表键值
访问注册表键值 在PowerShell中,用户可以通过类似于HKCU:(作为HKEY_CURRENT_USER)和HKLM:(代表HKEY_LOCAL_MATCHINE)的虚拟驱动器访问注册表键值. ...
- 通过程序修改注册表键值来达到修改IE配置参数的目的
通过程序修改注册表键值来达到修改IE配置参数的目的 使用IE访问应用程序或网页时经常需要设置一些选项(工具-Internet 选项),比如为了避免缓存网页,把工具-Internet选项-常规选项卡-I ...
- 使用Windows命令行reg控制注册表键值
使用Windows命令行reg控制注册表键值 引言 熟悉Windows操作系统的朋友可能都知道,Windows操作系统下的注册表相当于系统的数据库 ,部分软件将自己的配置信息都放在注册表里面,而注册表 ...
- Ubuntu - Dconf 注册表键值修改参考表
gsettings reset org.gnome.desktop.wm.preferences theme默认gnomegsettings set org.gnome.desktop.interfa ...
- NSIS:检查某注册表键是否存在
原文NSIS:检查某注册表键是否存在 ;定义注册表主键!define HKEY_CLASSES_ROOT 0x80000000!define HKEY_CURRENT_USER ...
- 如何查询注册表的值及 Powershell 应用
利用 c:\windows\system32\reg.exe 的 query 参数即可. reg.exe 的参数如下: C:\windows\system32> reg.exe /?REG Op ...
- Android fragment-findFragmentByTag 始终返回 null
我曾四处看看,在我的案子中找到几个与类似的主题,但没有帮助的问题.我想访问现有活动片段使用getSupportFragmentManager().findFragmentByTag(TAG),但它始终 ...
- 如何通过.reg文件来修改注册表键和子键以及键值
无废话, 直接上例子, 自己运行一下便知. 然后根据自己需要改改就可以随便用了. 添加key, subkey, 和设置键值的例子. ==================== Windows Reg ...
- SQL Server外键关系是强制约束,外键值也可以是空(NULL)
在SQL Server中,实际上外键值可不可以为空(NULL),和外键关系是不是强制约束无关. 我们先在SQL Server数据库中建立两张表People和Car,一个People可以有多个Car,所 ...
随机推荐
- 转 -- pydoc用法
原文地址: https://www.cnblogs.com/meitian/p/6704488.html pydoc用法 pydoc是python自带的一个文档生成工具,使用pydoc可以很方便的查看 ...
- 007、Docker 架构详解(2018-12-24 周一)
参考https://www.cnblogs.com/CloudMan6/p/6763789.html Docker核心组件包括: Docker 客户端 Docker Clien ...
- C++字符串(srtring)反转
对称平方数 题目描述 打印所有不超过n(n<256)的,其平方具有对称性质的数.如11*11=121. 分析: 1.首先使用函数to_string将int转化为string: 2.然后将字符串反 ...
- mysql select 时间戳转标准时间写法
select FROM_UNIXTIME(create_time, '%Y-%m-%d %H:%i:%S') as create_time from tabName
- mysql 原理 ~ 二阶段提交协议通说
一 简介: 今天是第二篇,讲解的是mysql的事务日志 二 具体 1 WAL技术(先写日志,再写磁盘) 2 binlog redolog 二阶段提交协议 目的 保持 redo log和binl ...
- ActiveMQ集成Spring使用
现在任何一个框架的使用都会结合spring框架,quartz.cxf与平时常见的Hibernate.mybatis.Struts等都可以与spring集成起来使用,在这里研究了activemq结合sp ...
- SpringBoot定制错误的Json数据
(1)自定义异常处理&返回定制Json数据 @ControllerAdvice public class MyExceptionHandler { @ResponseBody @Excepti ...
- 【CTF REVERSE】ctf02-查找字符串
1.前言 公司大拿给写的一个CTF逆向程序,提升我们组内人员的水平. 基于对话框MFC框架开发,使用EDIT控制特性隐藏Flag,可借助spy4win之类窗体工具找出Flag. 程序加UPX壳,已对壳 ...
- 【转】snprintf()函数使用方法
众所周知,sprintf不能检查目标字符串的长度,可能造成众多安全问题,所以都会推荐使用snprintf. 注:sprintf()函数:int sprintf( char *buffer, const ...
- Ubuntu下安装Golang并测试HelloWorld
Intel Core i5-8250U,Ubuntu 18.04(安装在虚拟机Oracle VirtualBox 5.2.12上),Go 1.11, 安装步骤如下: -进入Go文档官网: https: ...