| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WindowsRegistryStore.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Runtime.Versioning; |
| | | 8 | | using Microsoft.Win32; |
| | | 9 | | using MyNet.IO.Registry; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Platform.Windows.Registry; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Windows implementation of <see cref="IRegistryStore"/> using the Windows Registry. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="converter">The converter used to convert between .NET types and registry value types.</param> |
| | | 17 | | [SupportedOSPlatform("windows")] |
| | | 18 | | public sealed class WindowsRegistryStore(IRegistryValueConverter converter) : IRegistryStore |
| | | 19 | | { |
| | 24 | 20 | | private readonly RegistryKey _root = Microsoft.Win32.Registry.CurrentUser; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Sets a value in the Windows Registry at the specified path and value key. The value is converted to a registry-c |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="path">The registry path where the value will be stored.</param> |
| | | 26 | | /// <param name="valueKey">The name of the value to store.</param> |
| | | 27 | | /// <param name="value">The value to store.</param> |
| | | 28 | | /// <typeparam name="T">The type of the value to store.</typeparam> |
| | | 29 | | public void Set<T>(RegistryPath path, string valueKey, T value) |
| | | 30 | | { |
| | 42 | 31 | | using var key = _root.CreateSubKey(path.ToString()); |
| | 42 | 32 | | key.SetValue(valueKey, converter.ConvertTo(value!)); |
| | 84 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets a value from the Windows Registry at the specified path and value key. The method attempts to open the regi |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="path">The registry path where the value is stored.</param> |
| | | 39 | | /// <param name="valueKey">The name of the value to retrieve.</param> |
| | | 40 | | /// <typeparam name="T">The type of the value to retrieve.</typeparam> |
| | | 41 | | /// <returns>The retrieved value, or the default value if the key or value does not exist.</returns> |
| | | 42 | | public T? Get<T>(RegistryPath path, string valueKey) |
| | | 43 | | { |
| | 3 | 44 | | using var key = _root.OpenSubKey(path.ToString()); |
| | | 45 | | |
| | 3 | 46 | | var raw = key?.GetValue(valueKey); |
| | | 47 | | |
| | 3 | 48 | | return raw is null ? default : (T)converter.ConvertFrom(raw, typeof(T)); |
| | 3 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Checks if a registry key exists at the specified path in the Windows Registry. The method attempts to open the r |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="path">The registry path to check for existence.</param> |
| | | 55 | | /// <returns>True if the registry key exists, false otherwise.</returns> |
| | 6 | 56 | | public bool Exists(RegistryPath path) => _root.OpenSubKey(path.ToString()) is not null; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Removes a registry key and its subkeys at the specified path in the Windows Registry. The method attempts to del |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="path">The registry path of the key to remove.</param> |
| | 6 | 62 | | public void Remove(RegistryPath path) => _root.DeleteSubKeyTree(path.ToString(), false); |
| | | 63 | | } |
| | | 64 | | |