< Summary

Information
Class: MyNet.Platform.Windows.Registry.WindowsRegistryQuery
Assembly: MyNet.Platform.Windows
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Registry/WindowsRegistryQuery.cs
Tag: 342_31701152804
Line coverage
85%
Covered lines: 12
Uncovered lines: 2
Coverable lines: 14
Total lines: 52
Line coverage: 85.7%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
FindByValue(...)70%101084.61%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="WindowsRegistryQuery.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/// Implements a registry query using the Windows Registry API.
 15/// </summary>
 16[SupportedOSPlatform("windows")]
 17public sealed class WindowsRegistryQuery(IRegistryValueConverter converter) : IRegistryQuery
 18{
 2419    private readonly RegistryKey _root = Microsoft.Win32.Registry.CurrentUser;
 20
 21    /// <summary>
 22    /// Finds a registry key by searching for a specific value under a parent key.
 23    /// </summary>
 24    /// <param name="parent">The parent registry path.</param>
 25    /// <param name="valueKey">The name of the value to search for.</param>
 26    /// <param name="value">The value to match.</param>
 27    /// <typeparam name="T">The type of the value.</typeparam>
 28    /// <returns>The registry path of the matching key, or null if not found.</returns>
 29    public RegistryPath? FindByValue<T>(RegistryPath parent, string valueKey, T value)
 30    {
 631        using var key = _root.OpenSubKey(parent.ToString());
 632        if (key is null)
 333            return null;
 34
 1535        foreach (var childName in key.GetSubKeyNames())
 36        {
 637            using var child = key.OpenSubKey(childName);
 38
 639            var raw = child?.GetValue(valueKey);
 640            if (raw is null)
 041                continue;
 42
 643            var converted = converter.ConvertFrom(raw, typeof(T));
 44
 645            if (Equals(converted, value))
 346                return RegistryPath.Combine(parent.ToString(), childName);
 47        }
 48
 049        return null;
 650    }
 51}
 52