/// <summary> /// 写入注册表,如果指定项已经存在,则修改指定项的值 /// </summary> /// <param name="KeyType">注册表基项枚举</param> /// <param name="key">注册表项,不包括基项</param> /// <param name="name">值名称</param> /// <param name="value">值</param> /// <returns>返回布尔值,指定操作是否成功</returns> public bool SetValue(RegeditHelper.KeyType KeyType, string key, string name, string value) { try { RegistryKey rk = (RegistryKey)getRegistryKey(KeyType); RegistryKey rkt = rk.CreateSubKey(key); if (rkt != null) rkt.SetValue(name, value); else { throw (new Exception("要写入的项不存在")); } return true; } catch { return false; } }
/// <summary> /// 读取注册表 /// </summary> /// <param name="KeyType">注册表基项枚举</param> /// <param name="key">注册表项,不包括基项</param> /// <param name="name">值名称</param> /// <returns>返回字符串</returns> public string GetValue(RegeditHelper.KeyType KeyType, string key, string name) { try { RegistryKey rk = (RegistryKey)getRegistryKey(KeyType); RegistryKey rkt = rk.OpenSubKey(key); if (rkt != null) return rkt.GetValue(name).ToString(); else throw (new Exception("无法找到指定项")); } catch (Exception ex) { return ex.Message; } }
/// <summary> /// 返回RegistryKey对象 /// </summary> /// <param name="KeyType">注册表基项枚举</param> /// <returns></returns> private object getRegistryKey(RegeditHelper.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; }
/// <summary> /// 检索指定项关联的所有值 /// </summary> /// <param name="KeyType">基项枚举</param> /// <param name="key">指定项字符串</param> /// <returns>返回指定项关联的所有值的字符串数组</returns> public string[] GetValues(RegeditHelper.KeyType KeyType, string key) { RegistryKey rk = (RegistryKey)getRegistryKey(KeyType); RegistryKey rkt = rk.OpenSubKey(key); if (rkt != null) { 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; } } else { throw (new Exception("指定项不存在")); } }
/// <summary> /// 判断指定项是否存在 /// </summary> /// <param name="KeyType">基项枚举</param> /// <param name="key">指定项字符串</param> /// <returns>返回布尔值,说明指定项是否存在</returns> public bool IsExist(RegeditHelper.KeyType KeyType, string key) { RegistryKey rk = (RegistryKey)getRegistryKey(KeyType); if (rk.OpenSubKey(key) == null) return false; else return true; }
/// <summary> /// 删除注册表中的指定项 /// </summary> /// <param name="KeyType">注册表基项枚举</param> /// <param name="key">注册表中的项,不包括基项</param> /// <returns>返回布尔值,指定操作是否成功</returns> public bool DeleteSubKey(RegeditHelper.KeyType KeyType, string key) { try { RegistryKey rk = (RegistryKey)getRegistryKey(KeyType); rk.DeleteSubKeyTree(key); return true; } catch { return false; } }
/// <summary> /// 删除注册表中的值 /// </summary> /// <param name="KeyType">注册表基项枚举</param> /// <param name="key">注册表项名称,不包括基项</param> /// <param name="name">值名称</param> /// <returns>返回布尔值,指定操作是否成功</returns> public bool DeleteValue(RegeditHelper.KeyType KeyType, string key, string name) { try { RegistryKey rk = (RegistryKey)getRegistryKey(KeyType); RegistryKey rkt = rk.OpenSubKey(key, true); if (rkt != null) rkt.DeleteValue(name, true); else throw (new Exception("无法找到指定项")); return true; } catch { return false; } }