C# 64位系统 注册表的读写

/// <summary>
    /// 注册表
    /// </summary>
    public class RegistryKey64
    {
        #region 静态
        static IntPtr GetHiveHandle(RegistryHive hive)
        {
            IntPtr preexistingHandle = IntPtr.Zero;
            IntPtr HKEY_CLASSES_ROOT = new IntPtr(-2147483648);
            IntPtr HKEY_CURRENT_USER = new IntPtr(-2147483647);
            IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646);
            IntPtr HKEY_USERS = new IntPtr(-2147483645);
            IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(-2147483644);
            IntPtr HKEY_CURRENT_CONFIG = new IntPtr(-2147483643);
            IntPtr HKEY_DYN_DATA = new IntPtr(-2147483642);
            switch (hive)
            {
                case RegistryHive.ClassesRoot: preexistingHandle = HKEY_CLASSES_ROOT; break;
                case RegistryHive.CurrentUser: preexistingHandle = HKEY_CURRENT_USER; break;
                case RegistryHive.LocalMachine: preexistingHandle = HKEY_LOCAL_MACHINE; break;
                case RegistryHive.Users: preexistingHandle = HKEY_USERS; break;
                case RegistryHive.PerformanceData: preexistingHandle = HKEY_PERFORMANCE_DATA; break;
                case RegistryHive.CurrentConfig: preexistingHandle = HKEY_CURRENT_CONFIG; break;
                case RegistryHive.DynData: preexistingHandle = HKEY_DYN_DATA; break;
            }
            return preexistingHandle;
        }
        #endregion
        #region 构造
        public RegistryKey64(RegistryHive hive, RegistryView view)
        {
            this.View = view;
            this.Hive = hive;
            this.SubKey = "";
            rootHandle = new SafeRegistryHandle(GetHiveHandle(hive), true);//获得根节点的安全句柄
        }
        public RegistryKey64(RegistryHive hive)
        {
            this.View = Environment.Is64BitOperatingSystem?RegistryView.Registry64: RegistryView.Registry32;
            this.Hive = hive;
            this.SubKey = "";
            rootHandle = new SafeRegistryHandle(GetHiveHandle(hive), true);//获得根节点的安全句柄
        }
        #endregion
        #region 变量
        SafeRegistryHandle rootHandle = null;//顶级节点的句柄
        RegistryKey subKeyHandle = null;//当前子项的句柄
                   
        #endregion
        #region 属性
        /// <summary>
        /// 当前所在路径
        /// </summary>
        public string SubKey { get; private set; }
        /// <summary>
        /// 顶级路径
        /// </summary>
        public RegistryHive Hive { get; private set; }
        /// <summary>
        /// 视图模式
        /// </summary>
        public RegistryView View { get;private set; }
        /// <summary>
        /// 内部系统注册表信息
        /// </summary>
        public RegistryKey RegistryKey { get { return this.subKeyHandle ?? RegistryKey.FromHandle(this.rootHandle, this.View); } }
        #endregion
        /// <summary>
        /// 用于32位程序访问64位注册表
        /// </summary>
        /// <param name="valueName">项名称</param>
        /// <returns>值</returns>
        public object GetValue(string valueName)
        {
            return this.subKeyHandle.GetValue(valueName);//获得键下指定项的值
        }
        /// <summary>
        /// 打开子项
        /// </summary>
        /// <param name="subName">子项名称</param>
        public void OpenKey(string subName)
        {
            RegistryKey key = this.RegistryKey.OpenSubKey(subName);
            if(key==null)
                key=this.RegistryKey.CreateSubKey(subName);
            this.subKeyHandle = key;
            this.SubKey += "\\" + subName;
        }
        /// <summary>
        /// 用于32位的程序设置64位的注册表
        /// </summary>
        /// <param name="valueName">项名称</param>
        /// <param name="value">值</param>
        /// <param name="kind">值类型</param>
        public void SetValue(string valueName, object value, RegistryValueKind kind)
        {
            this.subKeyHandle.SetValue(valueName, value, kind);
        }
    }

RegistryKey64 key = new DTPM.Util.Configs.RegistryKey64(RegistryHive.LocalMachine);
key.OpenKey(@"SOFTWARE\XXX\XXX");
string path = key.GetValue("ConfigPath") as string;
key.RegistryKey.Close();


知识共享许可协议
《C# 64位系统 注册表的读写》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     170.29ms