using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Win32; namespace SteamTool.Core { public class RegistryKeyService { /// /// 创建注册表项 /// /// /// /// public RegistryKey CreateRegistryKey(RegistryKey registryKey, string path) { return registryKey.CreateSubKey(path); } /// /// 删除注册表项 /// /// /// /// public void DeleteRegistryKey(RegistryKey registryKey, string path) { registryKey.DeleteSubKey(path, true); //该方法无返回值,直接调用即可 registryKey.Close(); } /// /// 读取注册表值 /// /// /// /// /// public string ReadRegistryKey(RegistryKey registryKey, string path, string key) { var myreg = registryKey.OpenSubKey(path); if (myreg != null) { var info = myreg.GetValue(key)?.ToString(); myreg.Close(); return info; } return string.Empty; } /// /// 新增或修改注册表值 /// /// /// /// /// /// /// public void AddOrUpdateRegistryKey(RegistryKey registryKey, string path, string key, string value, RegistryValueKind registryValueKind) { RegistryKey myreg = registryKey.OpenSubKey(path, true); //该项必须已存在 if (myreg != null) { myreg.SetValue(key, value, registryValueKind); myreg.Close(); } } } }