using Microsoft.Win32; using System.Runtime.Versioning; // ReSharper disable once CheckNamespace namespace System { [SupportedOSPlatform("Windows")] public static class RegistryKeyExtensions { /// /// 读取注册表值 /// /// /// /// /// public static string Read(this RegistryKey registryKey, string path, string name) { var rk = registryKey.OpenSubKey(path); if (rk != null) { var value = rk.GetValue(name)?.ToString(); rk.Close(); return value ?? string.Empty; } return string.Empty; } /// /// 新增或修改注册表值 /// /// /// /// /// /// public static void AddOrUpdate(this RegistryKey registryKey, string path, string name, string value, RegistryValueKind valueKind) { var rk = registryKey.OpenSubKey(path, true); if (rk != null) // 该项必须已存在 { rk.SetValue(name, value, valueKind); rk.Close(); } } } #if DEBUG [Obsolete("use RegistryKeyExtensions", true)] public class RegistryKeyService { [Obsolete("use registryKey.Read", true)] public string ReadRegistryKey(RegistryKey registryKey, string path, string key) { return string.Empty; } [Obsolete("use registryKey.AddOrUpdate", true)] public void AddOrUpdateRegistryKey(RegistryKey registryKey, string path, string key, string value, RegistryValueKind registryValueKind) { } } #endif }