| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WindowsRegistryService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Runtime.Versioning; |
| | | 10 | | using MyNet.IO.Registry; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Platform.Windows.Registry; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Windows implementation of <see cref="IRegistryService"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="store">The registry store used to interact with the Windows Registry.</param> |
| | | 18 | | /// <param name="navigator">The registry navigator used to navigate the Windows Registry.</param> |
| | | 19 | | /// <param name="converter">The converter used to translate registry values to CLR property types.</param> |
| | | 20 | | [SupportedOSPlatform("windows")] |
| | | 21 | | public sealed class WindowsRegistryService(IRegistryStore store, IRegistryNavigator navigator, IRegistryValueConverter c |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Adds or updates a registry entry in the Windows Registry. The method takes a <see cref="RegistryEntry{T}"/> obje |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="entry">The registry entry to add or update.</param> |
| | | 27 | | /// <typeparam name="T">The type of the item in the registry entry.</typeparam> |
| | | 28 | | public void AddOrUpdate<T>(RegistryEntry<T> entry) |
| | | 29 | | { |
| | 72 | 30 | | foreach (var prop in typeof(T).GetProperties().Where(p => p.CanWrite)) |
| | | 31 | | { |
| | 27 | 32 | | var value = prop.GetValue(entry.Item); |
| | | 33 | | |
| | 27 | 34 | | if (value is null) |
| | | 35 | | continue; |
| | | 36 | | |
| | 27 | 37 | | store.Set(entry.Path, prop.Name, value); |
| | | 38 | | } |
| | 9 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Retrieves a registry entry from the Windows Registry based on the provided <see cref="RegistryPath"/>. The metho |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="path">The registry path of the entry to retrieve.</param> |
| | | 45 | | /// <typeparam name="T">The type of the item in the registry entry.</typeparam> |
| | | 46 | | /// <returns>The retrieved registry entry, or null if the key does not exist.</returns> |
| | | 47 | | public RegistryEntry<T>? Get<T>(RegistryPath path) |
| | | 48 | | where T : new() |
| | | 49 | | { |
| | 12 | 50 | | var instance = new T(); |
| | | 51 | | |
| | 12 | 52 | | using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path.ToString()); |
| | 12 | 53 | | if (key is null) |
| | 3 | 54 | | return null; |
| | | 55 | | |
| | 72 | 56 | | foreach (var prop in typeof(T).GetProperties().Where(p => p.CanWrite)) |
| | | 57 | | { |
| | 27 | 58 | | var raw = key.GetValue(prop.Name); |
| | 27 | 59 | | if (raw is null) |
| | | 60 | | continue; |
| | | 61 | | |
| | 27 | 62 | | var converted = converter.ConvertFrom(raw, prop.PropertyType); |
| | 27 | 63 | | prop.SetValue(instance, converted); |
| | | 64 | | } |
| | | 65 | | |
| | 9 | 66 | | return new RegistryEntry<T>(path, instance); |
| | 12 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Retrieves all registry entries under a specified parent path in the Windows Registry. The method uses the provid |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="parent">The parent registry path under which to retrieve entries.</param> |
| | | 73 | | /// <typeparam name="T">The type of the items in the registry entries.</typeparam> |
| | | 74 | | /// <returns>A collection of retrieved registry entries.</returns> |
| | | 75 | | public IEnumerable<RegistryEntry<T>> GetAll<T>(RegistryPath parent) |
| | 3 | 76 | | where T : new() => navigator.GetChildren(parent) |
| | 3 | 77 | | .Select(Get<T>) |
| | 3 | 78 | | .Where(x => x is not null)!; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Removes a registry entry from the Windows Registry based on the provided <see cref="RegistryPath"/>. The method |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="path">The registry path of the entry to remove.</param> |
| | 3 | 84 | | public void Remove(RegistryPath path) => store.Remove(path); |
| | | 85 | | } |
| | | 86 | | |