From da10a2b1324e3ae6d6e8276cadd3d91b7cc51516 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com>
Date: Sat, 18 Jul 2026 01:23:54 +0800
Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=20RegistryUtils?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Shared/Utils/RegistryUtils.cs | 71 +++++++++++++++--------------------
1 file changed, 30 insertions(+), 41 deletions(-)
diff --git a/Shared/Utils/RegistryUtils.cs b/Shared/Utils/RegistryUtils.cs
index 33b84e7..49587d2 100644
--- a/Shared/Utils/RegistryUtils.cs
+++ b/Shared/Utils/RegistryUtils.cs
@@ -6,79 +6,68 @@ public static class RegistryUtils {
public const string NULL_STRING = "__NULL__";
///
- /// 读取注册表键。
+ /// 读取注册表值。
/// 若键值为 ,则返回 null。
///
- public static (object? result, bool exists) Read(string keyPath) {
- var (hive, subKeyPath, valueName) = ParsePath(keyPath);
- using RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default);
- using RegistryKey? subKey = baseKey.OpenSubKey(subKeyPath);
- var result = subKey?.GetValue(valueName);
+ public static (object? result, bool exists) Read(string folder, string entry) {
+ using RegistryKey? folderKey = GetRegistryKey(folder);
+ var result = folderKey?.GetValue(entry);
if (result is null) return (null, false);
return (result is string str && str == NULL_STRING ? null : result, true);
}
///
- /// 读取注册表键,若该键不存在或发生异常则返回 。
+ /// 读取注册表值,若该值不存在或发生异常则返回 。
/// 若键值为 ,则返回 null。
///
- public static object? TryRead(string keyPath, object? defaultValue = null) {
+ public static object? TryRead(string folder, string entry, object? defaultValue = null) {
try {
- var (result, exists) = Read(keyPath);
+ var (result, exists) = Read(folder, entry);
return exists ? result : defaultValue;
} catch (Exception ex) {
- Logger.Warn(ex, $"读取注册表键失败({keyPath})");
+ Logger.Warn(ex, $"读取注册表值失败({folder},{entry})");
return defaultValue;
}
}
///
- /// 写入注册表键。
+ /// 写入注册表值。
/// 会自动创建对应的子键。
/// 若 为 null,则写入 。
///
- public static void Write(string keyPath, object? value) {
- var (hive, subKeyPath, valueName) = ParsePath(keyPath);
- using RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default);
- using RegistryKey subKey = baseKey.CreateSubKey(subKeyPath);
- subKey.SetValue(valueName, value ?? NULL_STRING);
+ public static void Write(string folder, string entry, object? value) {
+ using RegistryKey folderKey = GetRegistryKey(folder, create: true)!;
+ folderKey.SetValue(entry, value ?? NULL_STRING);
}
///
- /// 判断对应的注册表键是否存在。
+ /// 判断对应的注册表值是否存在。
///
- public static bool Has(string keyPath) {
- var (hive, subKeyPath, valueName) = ParsePath(keyPath);
- using RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default);
- using RegistryKey? subKey = baseKey.OpenSubKey(subKeyPath);
- if (subKey is null) return false;
+ public static bool Has(string folder, string entry) {
+ using RegistryKey? folderKey = GetRegistryKey(folder);
+ if (folderKey is null) return false;
object notFound = new();
- return !ReferenceEquals(subKey.GetValue(valueName, notFound), notFound);
+ return !ReferenceEquals(folderKey.GetValue(entry, notFound), notFound);
}
///
- /// 删除注册表键。若不存在则不执行操作。
+ /// 删除注册表值。若不存在则不执行操作。
///
- public static void Delete(string keyPath) {
- var (hive, subKeyPath, valueName) = ParsePath(keyPath);
- using RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default);
- using RegistryKey? subKey = baseKey.OpenSubKey(subKeyPath, writable: true);
- subKey?.DeleteValue(valueName, throwOnMissingValue: false);
+ public static void Delete(string folder, string entry) {
+ using RegistryKey? folderKey = GetRegistryKey(folder, writable: true);
+ folderKey?.DeleteValue(entry, throwOnMissingValue: false);
}
- private static (RegistryHive Hive, string SubKeyPath, string ValueName) ParsePath(string fullPath) {
- int hiveEnd = fullPath.IndexOf('\\');
- int valueStart = fullPath.LastIndexOf('\\');
- if (hiveEnd <= 0 || hiveEnd > valueStart) throw new ArgumentException($"注册表路径不完整({fullPath})", nameof(fullPath));
- RegistryHive hive = fullPath.Substring(0, hiveEnd).ToUpperInvariant() switch {
- "HKEY_CLASSES_ROOT" => RegistryHive.ClassesRoot,
- "HKEY_CURRENT_USER" => RegistryHive.CurrentUser,
- "HKEY_LOCAL_MACHINE" => RegistryHive.LocalMachine,
- "HKEY_USERS" => RegistryHive.Users,
- "HKEY_CURRENT_CONFIG" => RegistryHive.CurrentConfig,
+ private static RegistryKey? GetRegistryKey(string fullPath, bool writable = false, bool create = false) {
+ using RegistryKey key = fullPath.BeforeFirst("\\").ToUpperInvariant() switch {
+ "HKEY_CLASSES_ROOT" => Registry.ClassesRoot,
+ "HKEY_CURRENT_USER" => Registry.CurrentUser,
+ "HKEY_LOCAL_MACHINE" => Registry.LocalMachine,
+ "HKEY_USERS" => Registry.Users,
+ "HKEY_CURRENT_CONFIG" => Registry.CurrentConfig,
_ => throw new ArgumentException($"注册表路径有误({fullPath})", nameof(fullPath))
};
- string subKeyPath = valueStart == hiveEnd ? "" : fullPath.Substring(hiveEnd + 1, valueStart - hiveEnd - 1);
- return (hive, subKeyPath, fullPath.Substring(valueStart + 1));
+ string subKey = fullPath.AfterFirst("\\");
+ return create ? key.CreateSubKey(subKey) : key.OpenSubKey(subKey, writable);
}
}