標籤:style blog io ar color os 使用 sp for
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();
如果大家有改進,請留言,謝謝
C# 64位系統 註冊表的讀寫