注册表键值明明存在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,所 ...
随机推荐
- 06-开闭原则(OCP)
1. 背景 在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不对整个功能进行重构,并且需要原有代码经过重新测试. 2. 定义 ...
- ECSHOP /mobile/admin/edit_languages.php
漏洞名称:ecshop代码注入漏洞 补丁编号:10017531 补丁文件:/mobile/admin/edit_languages.php 补丁来源:云盾自研 更新时间:2017-01-05 08:4 ...
- c#中如何在一个panel中放入窗体
Form2 f2 = new Form2(); //实例化窗体FORM2 f2.TopLevel = false; //设置为非顶级窗体 f2.FormBorderStyle = FormBorder ...
- 第15月第6天 ios UIScrollView不能响应TouchesBegin
1. 1:@property MyScrollView *scrollView; 2:给MyScrollView,增加类别:MyScrollView+Touch 3:在类别里实现下面三个方法: @im ...
- python线程,pipe管道通信原理
Pipe管道: * 管道实例化后会产生两个通道,分别交给两个进程* 通过send和recv来交互数据,这是一个双向的管道,child和parent可以互相收发 from multiprocessing ...
- 微信小程序开发工具的基本应用
全局配置: 1.1配置所有页面路径:在app.json的{pages:[配置所有页面]},将首页放置在第一位,在app.json必须写上所有页面的路径,要不然会报错,每个页面的wxss样式文件只在当前 ...
- asp.net mvc 中[Authorize]在IE9以上版本关于FormsAuthentication.SetAuthCookie无效的问题 解决方案
简单的解决方法是,在网站根目录,新增一个浏览器定义文件(browser definition file) 叫“App_Browsers”文件夹,然后里面放一个“IE10.browser”文件即可,网站 ...
- Android:(本地、可通信的、前台、远程)Service使用全面介绍
2.具体使用解析 2.1 本地Service 这是最普通.最常用的后台服务Service. 2.1.1 使用步骤 步骤1:新建子类继承Service类 需重写父类的onCreate().onStart ...
- linux 下程序员专用搜索源码用来替代grep的软件ack(后来发现一个更快的: ag), 且有vim插件的
发现一个比ack更快更好用的: https://github.com/ggreer/the_silver_searcher , 使用时命令为ag,它是基于ack的代码二次开发的,所有使用方法基本 ...
- passwd: Have exhausted maximum number of retries for service【转】
使用命令passwd修改密码时,遇到如下问题: # echo 'utf8'|passwd zhangsan --stdin Changing password for user zhangsan. p ...