zoukankan      html  css  js  c++  java
  • 注册表键值明明存在OpenSubKey始终返回null,解决方案

    先上代码及实例
    RegistryKey rsg = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMacromediaFlashPaper Printer2Installation", 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 "SOFTWAREMyAppSettings" 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);
            }
        }
    }
    View Code


    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 = @"SOFTWAREMyAppSettings";
                string keyName = "MyAppConnectionStringKey";
    
                object connectionString = RegistryHelpers.GetRegistryValue(keyPath, keyName);
    
                Console.WriteLine(connectionString);
                Console.ReadLine();
            }
        }
    }
    View Code
    
    


    Output:

     
     
  • 相关阅读:
    failed: unacceptable content-type: text/html
    iOS button点击更换图片
    支付宝ios SDK官方下载页面
    xcode6 中使用OC代码时,在NSObject的子类中报错
    CocoaPods安装和使用教程
    Mac 下安装Ruby环境
    iOS .a与.framewofk
    Couldn't find preset "es2015" relative to directory问题解决
    yarn依赖管理工具的使用
    java.io.IOException: Could not delete path 'D:mycode eactnativeSecondTestandroidappuildgeneratedsource eleaseandroidsupportv7
  • 原文地址:https://www.cnblogs.com/xinweichen/p/5276934.html
Copyright © 2011-2022 走看看