/// <summary> /// 得到指定注冊表鍵,不存在則返回Null值 /// </summary> /// <param name="RegistryHive">注冊表巢</param> /// <param name="RegURL">子鍵位置,不含注冊表巢,各層間請用'\'隔開</param> /// <returns>指定注冊表鍵</returns> private static RegistryKey OpenRegKey(tagRegistryHive RegistryHive, string RegURL) { RegistryKey MainKey = null; RegistryKey SubKey = null; if (RegURL != "" && RegURL.Substring(RegURL.Length - 1) == "\\") RegURL = RegURL.Substring(0, RegURL.Length - 1); //得到注冊表巢 switch (RegistryHive) { case tagRegistryHive.DD: MainKey = Registry.DynData; break; case tagRegistryHive.HKCF: MainKey = Registry.CurrentConfig; break; case tagRegistryHive.HKCR: MainKey = Registry.ClassesRoot; break; case tagRegistryHive.HKCU: MainKey = Registry.CurrentUser; break; case tagRegistryHive.HKLM: MainKey = Registry.LocalMachine; break; case tagRegistryHive.HKUSR: MainKey = Registry.Users; break; case tagRegistryHive.PD: MainKey = Registry.PerformanceData; break; } SubKey = MainKey; try { if (RegURL != "") { string[] SubKeys = RegURL.Split('\\'); foreach (string _SubKey in SubKeys) { if (SubKey == null) return null; SubKey = SubKey.OpenSubKey(_SubKey); } } } catch (System.Exception e) { throw new Exception(e.Message, e); } return SubKey; }
/// <summary> /// 得到指定注冊表鍵值並解密,不存在返回Null值 /// </summary> /// <param name="RegistryHive">注冊表巢</param> /// <param name="RegURL">子鍵位置,不含注冊表巢,各層間請用'\'隔開</param> /// <returns></returns> public static object GetKeyValueDecrypt(tagRegistryHive RegistryHive, string RegURL) { RegistryKey MainKey = null; string SubKey = string.Empty; if (RegURL.Substring(RegURL.Length - 1) == "\\") RegURL = RegURL.Substring(0, RegURL.Length - 1); int LastIndex = RegURL.LastIndexOf('\\'); if (LastIndex <= 0) { MainKey = OpenRegKey(RegistryHive, ""); SubKey = RegURL; } else { MainKey = OpenRegKey(RegistryHive, RegURL.Substring(0, LastIndex)); SubKey = RegURL.Substring(LastIndex + 1); } if (MainKey == null) return null; else { object obj = MainKey.GetValue(SubKey); if (obj == null) return null; string _Result = null; MainKey.Close(); try { _Result = SymmetricMethod.Decrypt(obj.ToString()); } catch (System.Exception e) { throw new Exception(e.Message, e); } return _Result; } }
/// <summary> /// 得到指定注冊表鍵值,不存在返回Null值 /// </summary> /// <param name="RegistryHive">注冊表巢</param> /// <param name="RegURL">子鍵位置,不含注冊表巢,各層間請用'\'隔開</param> /// <returns></returns> public static object GetKeyValue(tagRegistryHive RegistryHive, string RegURL) { RegistryKey MainKey = null; string SubKey = string.Empty; if (RegURL.Substring(RegURL.Length - 1) == "\\") RegURL = RegURL.Substring(0, RegURL.Length - 1); int LastIndex = RegURL.LastIndexOf('\\'); try { if (LastIndex <= 0) { MainKey = OpenRegKey(RegistryHive, ""); SubKey = RegURL; } else { MainKey = OpenRegKey(RegistryHive, RegURL.Substring(0, LastIndex)); SubKey = RegURL.Substring(LastIndex + 1); } if (MainKey == null) return null; else { object obj = MainKey.GetValue(SubKey); //Added by Donnie on 2012/09/26 //如果是Default鍵值,則無法抓到,需要用null再抓一次 if (obj == null) obj = MainKey.GetValue(null); MainKey.Close(); return obj; } } catch (System.Exception e) { throw new Exception(e.Message, e); } }