< Summary

Information
Class: MyNet.Platform.Windows.Registry.WindowsRegistryService
Assembly: MyNet.Platform.Windows
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Registry/WindowsRegistryService.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 86
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddOrUpdate(...)100%66100%
Get(...)100%88100%
GetAll(...)50%22100%
Remove(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Registry/WindowsRegistryService.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="WindowsRegistryService.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Collections.Generic;
 8using System.Linq;
 9using System.Runtime.Versioning;
 10using MyNet.IO.Registry;
 11
 12namespace 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")]
 21public 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    {
 7230        foreach (var prop in typeof(T).GetProperties().Where(p => p.CanWrite))
 31        {
 2732            var value = prop.GetValue(entry.Item);
 33
 2734            if (value is null)
 35                continue;
 36
 2737            store.Set(entry.Path, prop.Name, value);
 38        }
 939    }
 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    {
 1250        var instance = new T();
 51
 1252        using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(path.ToString());
 1253        if (key is null)
 354            return null;
 55
 7256        foreach (var prop in typeof(T).GetProperties().Where(p => p.CanWrite))
 57        {
 2758            var raw = key.GetValue(prop.Name);
 2759            if (raw is null)
 60                continue;
 61
 2762            var converted = converter.ConvertFrom(raw, prop.PropertyType);
 2763            prop.SetValue(instance, converted);
 64        }
 65
 966        return new RegistryEntry<T>(path, instance);
 1267    }
 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)
 376        where T : new() => navigator.GetChildren(parent)
 377            .Select(Get<T>)
 378            .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>
 384    public void Remove(RegistryPath path) => store.Remove(path);
 85}
 86