< Summary

Information
Class: MyNet.Platform.Windows.Registry.WindowsRegistryStore
Assembly: MyNet.Platform.Windows
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Registry/WindowsRegistryStore.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 64
Line coverage: 100%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Set(...)100%11100%
Get(...)50%44100%
Exists(...)100%11100%
Remove(...)100%11100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="WindowsRegistryStore.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Runtime.Versioning;
 8using Microsoft.Win32;
 9using MyNet.IO.Registry;
 10
 11namespace 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")]
 18public sealed class WindowsRegistryStore(IRegistryValueConverter converter) : IRegistryStore
 19{
 2420    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    {
 4231        using var key = _root.CreateSubKey(path.ToString());
 4232        key.SetValue(valueKey, converter.ConvertTo(value!));
 8433    }
 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    {
 344        using var key = _root.OpenSubKey(path.ToString());
 45
 346        var raw = key?.GetValue(valueKey);
 47
 348        return raw is null ? default : (T)converter.ConvertFrom(raw, typeof(T));
 349    }
 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>
 656    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>
 662    public void Remove(RegistryPath path) => _root.DeleteSubKeyTree(path.ToString(), false);
 63}
 64