Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion Include="GitHubActionsTestLogger" Version="3.0.3" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Gress" Version="2.1.1" />
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="PolyShim" Version="2.8.2" />
<PackageVersion Include="xunit" Version="2.9.2" />
Expand Down
47 changes: 47 additions & 0 deletions PowerKit.Tests/RegistryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Runtime.Versioning;
using FluentAssertions;
using Microsoft.Win32;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

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

// Arrange
Comment thread
Tyrrrz marked this conversation as resolved.
using var key = Registry.CurrentUser.OpenSubKey("Software", true)!;
var subKeyName = $"PowerKit.Tests.{Guid.NewGuid():N}";

try
{
using var subKey = key.CreateSubKey(subKeyName);

// Act & assert
key.ContainsSubKey(subKeyName).Should().BeTrue();
}
finally
{
key.DeleteSubKeyTree(subKeyName, false);
}
}

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

// Arrange
using var key = Registry.CurrentUser.OpenSubKey("Software", false)!;
Comment thread
Tyrrrz marked this conversation as resolved.

// Act & assert
key.ContainsSubKey("this-sub-key-definitely-does-not-exist").Should().BeFalse();
}
}
20 changes: 20 additions & 0 deletions PowerKit/Extensions/RegistryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Versioning;
using Microsoft.Win32;

namespace PowerKit.Extensions;

internal static class RegistryExtensions
{
extension(RegistryKey key)
{
/// <summary>
/// Checks whether a sub-key with the specified name exists under the registry key.
/// </summary>
[SupportedOSPlatform("windows")]
public bool ContainsSubKey(string name)
Comment thread
Tyrrrz marked this conversation as resolved.
{
using var subKey = key.OpenSubKey(name, false);
Comment thread
Tyrrrz marked this conversation as resolved.
return subKey is not null;
}
}
}
1 change: 1 addition & 0 deletions PowerKit/PowerKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" />
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
<PackageReference Include="PolyShim" />
</ItemGroup>
</Project>