Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions PowerKit.Tests/Extensions/RegistryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ namespace PowerKit.Tests.Extensions;

public class RegistryExtensionsTests
{
[SkippableFact]
[SupportedOSPlatform("windows")]
public void OpenKey_Test()
{
Skip.IfNot(OperatingSystem.IsWindows());

// Act
using var key = RegistryHive.CurrentUser.OpenKey(RegistryView.Default);

// Assert
key.Should().NotBeNull();
key.Name.Should().Be(Registry.CurrentUser.Name);
}

[SkippableFact]
[SupportedOSPlatform("windows")]
public void ContainsSubKey_Exists_Test()
Expand Down
29 changes: 29 additions & 0 deletions PowerKit/Extensions/RegistryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if NETFRAMEWORK || NET5_0_OR_GREATER
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using Microsoft.Win32;
Expand All @@ -11,6 +12,34 @@ namespace PowerKit.Extensions;
#endif
internal static class RegistryExtensions
{
extension(RegistryHive hive)
{
/// <summary>
/// Returns the short moniker for the registry hive (e.g. <c>HKCU</c>, <c>HKLM</c>).
/// </summary>
[SupportedOSPlatform("windows")]
public string Moniker =>
hive switch
{
RegistryHive.ClassesRoot => "HKCR",
RegistryHive.CurrentUser => "HKCU",
RegistryHive.LocalMachine => "HKLM",
RegistryHive.Users => "HKU",
RegistryHive.PerformanceData => "HKPD",
RegistryHive.CurrentConfig => "HKCC",
_ => throw new ArgumentOutOfRangeException(nameof(hive)),
};

#if NET40_OR_GREATER || NET5_0_OR_GREATER
/// <summary>
/// Opens the base registry key for the hive using the specified <paramref name="view" />.
/// </summary>
[SupportedOSPlatform("windows")]
public RegistryKey OpenKey(RegistryView view = RegistryView.Default) =>
RegistryKey.OpenBaseKey(hive, view);
#endif
}

extension(RegistryKey key)
{
/// <summary>
Expand Down