64位系统,读取时是要有区别的,写入时没有问题。这里的代码时通用的,已经内部判断当前系统的位数。
1 /// <summary> 2 /// 注册表 3 /// </summary> 4 public class RegistryKey64 5 { 6 #region 静态 7 static IntPtr GetHiveHandle(RegistryHive hive) 8 { 9 IntPtr preexistingHandle = IntPtr.Zero; 10 11 IntPtr HKEY_CLASSES_ROOT = new IntPtr(-2147483648); 12 IntPtr HKEY_CURRENT_USER = new IntPtr(-2147483647); 13 IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646); 14 IntPtr HKEY_USERS = new IntPtr(-2147483645); 15 IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(-2147483644); 16 IntPtr HKEY_CURRENT_CONFIG = new IntPtr(-2147483643); 17 IntPtr HKEY_DYN_DATA = new IntPtr(-2147483642); 18 switch (hive) 19 { 20 case RegistryHive.ClassesRoot: preexistingHandle = HKEY_CLASSES_ROOT; break; 21 case RegistryHive.CurrentUser: preexistingHandle = HKEY_CURRENT_USER; break; 22 case RegistryHive.LocalMachine: preexistingHandle = HKEY_LOCAL_MACHINE; break; 23 case RegistryHive.Users: preexistingHandle = HKEY_USERS; break; 24 case RegistryHive.PerformanceData: preexistingHandle = HKEY_PERFORMANCE_DATA; break; 25 case RegistryHive.CurrentConfig: preexistingHandle = HKEY_CURRENT_CONFIG; break; 26 case RegistryHive.DynData: preexistingHandle = HKEY_DYN_DATA; break; 27 } 28 return preexistingHandle; 29 } 30 #endregion 31 32 #region 构造 33 public RegistryKey64(RegistryHive hive, RegistryView view) 34 { 35 this.View = view; 36 this.Hive = hive; 37 this.SubKey = ""; 38 rootHandle = new SafeRegistryHandle(GetHiveHandle(hive), true);//获得根节点的安全句柄 39 } 40 public RegistryKey64(RegistryHive hive) 41 { 42 this.View = Environment.Is64BitOperatingSystem?RegistryView.Registry64: RegistryView.Registry32; 43 this.Hive = hive; 44 this.SubKey = ""; 45 rootHandle = new SafeRegistryHandle(GetHiveHandle(hive), true);//获得根节点的安全句柄 46 } 47 #endregion 48 49 #region 变量 50 SafeRegistryHandle rootHandle = null;//*节点的句柄 51 RegistryKey subKeyHandle = null;//当前子项的句柄 52 53 #endregion 54 55 #region 属性 56 /// <summary> 57 /// 当前所在路径 58 /// </summary> 59 public string SubKey { get; private set; } 60 /// <summary> 61 /// *路径 62 /// </summary> 63 public RegistryHive Hive { get; private set; } 64 /// <summary> 65 /// 视图模式 66 /// </summary> 67 public RegistryView View { get;private set; } 68 /// <summary> 69 /// 内部系统注册表信息 70 /// </summary> 71 public RegistryKey RegistryKey { get { return this.subKeyHandle ?? RegistryKey.FromHandle(this.rootHandle, this.View); } } 72 #endregion 73 /// <summary> 74 /// 用于32位程序访问64位注册表 75 /// </summary> 76 /// <param name="valueName">项名称</param> 77 /// <returns>值</returns> 78 public object GetValue(string valueName) 79 { 80 return this.subKeyHandle.GetValue(valueName);//获得键下指定项的值 81 } 82 /// <summary> 83 /// 打开子项 84 /// </summary> 85 /// <param name="subName">子项名称</param> 86 public void OpenKey(string subName) 87 { 88 RegistryKey key = this.RegistryKey.OpenSubKey(subName); 89 if(key==null) 90 key=this.RegistryKey.CreateSubKey(subName); 91 this.subKeyHandle = key; 92 this.SubKey += "\\" + subName; 93 } 94 /// <summary> 95 /// 用于32位的程序设置64位的注册表 96 /// </summary> 97 /// <param name="valueName">项名称</param> 98 /// <param name="value">值</param> 99 /// <param name="kind">值类型</param> 100 public void SetValue(string valueName, object value, RegistryValueKind kind) 101 { 102 this.subKeyHandle.SetValue(valueName, value, kind); 103 } 104 }
使用方法
1 RegistryKey64 key = new DTPM.Util.Configs.RegistryKey64(RegistryHive.LocalMachine); 2 key.OpenKey(@"SOFTWARE\XXX\XXX"); 3 string path = key.GetValue("ConfigPath") as string; 4 key.RegistryKey.Close();
如果大家有改进,请留言,谢谢