using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; using System.Reflection; namespace 电子展板.Utility.Core { #if NETFRAMEWORK /// /// 注册表辅助类 /// public class RegisterHelper { /// /// 默认注册表基项 /// private string baseKey = "Software"; #region 构造函数 /// /// 构造函数 /// /// 基项的名称 public RegisterHelper() { } /// /// 构造函数 /// /// 基项的名称 public RegisterHelper(string baseKey) { this.baseKey = baseKey; } #endregion #region 公共方法 /// /// 写入注册表,如果指定项已经存在,则修改指定项的值 /// /// 注册表基项枚举 /// 注册表项,不包括基项 /// 值名称 /// 值 public void SetValue(KeyType keytype, string key, string name, string values) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.CreateSubKey(key); if (rkt != null) { rkt.SetValue(name, values); } } /// /// 读取注册表 /// /// 注册表基项枚举 /// 注册表项,不包括基项 /// 值名称 /// 返回字符串 public string GetValue(KeyType keytype, string key, string name) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.OpenSubKey(key); if (rkt != null) { return rkt.GetValue(name).ToString(); } else { return string.Empty; } } /// /// 删除注册表中的值 /// /// 注册表基项枚举 /// 注册表项名称,不包括基项 /// 值名称 public void DeleteValue(KeyType keytype, string key, string name) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.OpenSubKey(key, true); if (rkt != null) { object value = rkt.GetValue(name); if (value != null) { rkt.DeleteValue(name, true); } } } /// /// 删除注册表中的指定项 /// /// 注册表基项枚举 /// 注册表中的项,不包括基项 /// 返回布尔值,指定操作是否成功 public void DeleteSubKey(KeyType keytype, string key) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); if (software != null) { software.DeleteSubKeyTree(key); } } /// /// 判断指定项是否存在 /// /// 基项枚举 /// 指定项字符串 /// 返回布尔值,说明指定项是否存在 public bool IsExist(KeyType keytype, string key) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey); RegistryKey rkt = software.OpenSubKey(key); if (rkt != null) { return true; } else { return false; } } /// /// 检索指定项关联的所有值 /// /// 基项枚举 /// 指定项字符串 /// 返回指定项关联的所有值的字符串数组 public string[] GetValues(KeyType keytype, string key) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.OpenSubKey(key); string[] names = rkt.GetValueNames(); if (names.Length == 0) { return names; } else { string[] values = new string[names.Length]; int i = 0; foreach (string name in names) { values[i] = rkt.GetValue(name).ToString(); i++; } return values; } } /// /// 将对象所有属性写入指定注册表中 /// /// 注册表基项枚举 /// 注册表项,不包括基项 /// 传入的对象 public void SetObjectValue(KeyType keyType, string key, Object obj) { if (obj != null) { Type t = obj.GetType(); string name; object value; foreach (var p in t.GetProperties()) { if (p != null) { name = p.Name; value = p.GetValue(obj, null); this.SetValue(keyType, key, name, value.ToString()); } } } } #endregion #region 私有方法 /// /// 返回RegistryKey对象 /// /// 注册表基项枚举 /// private object GetRegistryKey(KeyType keyType) { RegistryKey rk = null; switch (keyType) { case KeyType.HKEY_CLASS_ROOT: rk = Registry.ClassesRoot; break; case KeyType.HKEY_CURRENT_USER: rk = Registry.CurrentUser; break; case KeyType.HKEY_LOCAL_MACHINE: rk = Registry.LocalMachine; break; case KeyType.HKEY_USERS: rk = Registry.Users; break; case KeyType.HKEY_CURRENT_CONFIG: rk = Registry.CurrentConfig; break; } return rk; } #endregion #region 枚举 /// /// 注册表基项枚举 /// public enum KeyType : int { /// /// 注册表基项 HKEY_CLASSES_ROOT /// HKEY_CLASS_ROOT, /// /// 注册表基项 HKEY_CURRENT_USER /// HKEY_CURRENT_USER, /// /// 注册表基项 HKEY_LOCAL_MACHINE /// HKEY_LOCAL_MACHINE, /// /// 注册表基项 HKEY_USERS /// HKEY_USERS, /// /// 注册表基项 HKEY_CURRENT_CONFIG /// HKEY_CURRENT_CONFIG } #endregion } #endif }