Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ jobs:
echo "Building $sln"
dotnet build "$sln" --configuration Release
echo "Testing $sln (no-op for folders with no test project)"
dotnet test "$sln" --configuration Release --no-build
dotnet test "$sln" --configuration Release --no-build --filter "FullyQualifiedName!~Live"

# Stable, folder-independent name for branch protection to require -- the
# build-and-test job's displayed name varies with the matrix (one leg per
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public static string GetCurrentUserRootKeyName()
{
return Registry.CurrentUser.Name;

Check warning on line 15 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/ReadWriteWindowsRegistryInCSharp/RegistryDemo.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'RegistryKey.Name' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 15 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/ReadWriteWindowsRegistryInCSharp/RegistryDemo.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'Registry.CurrentUser' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 15 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/ReadWriteWindowsRegistryInCSharp/RegistryDemo.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'RegistryKey.Name' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 15 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/ReadWriteWindowsRegistryInCSharp/RegistryDemo.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'Registry.CurrentUser' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}

public static string GetCurrentUserRootKeyNameWithPlatformCheck()
Expand Down Expand Up @@ -42,7 +42,7 @@
return string.Empty;
}

var subKeyToWrite = Path.Combine(Registry.CurrentUser.Name, CodeMazeRegistryDemoSubKey);
var subKeyToWrite = $@"{Registry.CurrentUser.Name}\{CodeMazeRegistryDemoSubKey}";

Registry.SetValue(subKeyToWrite, CodeMazeRegistryDemoName, CodeMazeRegistryDemoValue);
var writtenValue = Registry.GetValue(subKeyToWrite, CodeMazeRegistryDemoName, string.Empty);
Expand All @@ -64,9 +64,9 @@
using var subKey = baseKey.OpenSubKey(CodeMazeRegistryDemoSubKey, true) ??
baseKey.CreateSubKey(CodeMazeRegistryDemoSubKey);

subKey?.SetValue(CodeMazeRegistryDemoName, CodeMazeRegistryDemoValue);
var writtenValue = subKey?.GetValue(CodeMazeRegistryDemoName);
subKey?.DeleteValue(CodeMazeRegistryDemoName);
subKey.SetValue(CodeMazeRegistryDemoName, CodeMazeRegistryDemoValue);
var writtenValue = subKey.GetValue(CodeMazeRegistryDemoName);
subKey.DeleteValue(CodeMazeRegistryDemoName);

baseKey.DeleteSubKey(CodeMazeRegistryDemoSubKey);

Expand All @@ -77,37 +77,37 @@
{
if (!OperatingSystem.IsWindows())
{
return Array.Empty<string>();
return [];
}

using var subKey = Registry.CurrentUser.CreateSubKey(CodeMazeRegistryDemoSubKey);
subKey?.CreateSubKey("SubKey1");
subKey?.CreateSubKey("SubKey2");
subKey.CreateSubKey("SubKey1");
subKey.CreateSubKey("SubKey2");

var subKeyNames = subKey?.GetSubKeyNames();
var subKeyNames = subKey.GetSubKeyNames();

Registry.CurrentUser.DeleteSubKeyTree(CodeMazeRegistryDemoSubKey);

return subKeyNames ?? Array.Empty<string>();
return subKeyNames;
}

public static string[] GetValueNames()
{
if (!OperatingSystem.IsWindows())
{
return Array.Empty<string>();
return [];
}

using var subKey = Registry.CurrentUser.CreateSubKey(CodeMazeRegistryDemoSubKey);
using var subKey1 = subKey?.CreateSubKey("SubKey1");
subKey1?.SetValue("Name1", "Value1");
subKey1?.SetValue("Name2", "Value2");
using var subKey1 = subKey.CreateSubKey("SubKey1");
subKey1.SetValue("Name1", "Value1");
subKey1.SetValue("Name2", "Value2");

var subKeyNames = subKey1?.GetValueNames();
var subKeyNames = subKey1.GetValueNames();

Registry.CurrentUser.DeleteSubKeyTree(CodeMazeRegistryDemoSubKey);

return subKeyNames ?? Array.Empty<string>();
return subKeyNames;
}

public static string GetValueKind()
Expand All @@ -118,14 +118,14 @@
}

using var subKey = Registry.CurrentUser.CreateSubKey(CodeMazeRegistryDemoSubKey);
using var subKey1 = subKey?.CreateSubKey("SubKey1");
subKey1?.SetValue("Name1", "Value1");
using var subKey1 = subKey.CreateSubKey("SubKey1");
subKey1.SetValue("Name1", "Value1");

var valueKind = subKey1?.GetValueKind("Name1");
var valueKind = subKey1.GetValueKind("Name1");

Registry.CurrentUser.DeleteSubKeyTree(CodeMazeRegistryDemoSubKey);

return valueKind.ToString() ?? string.Empty;
return valueKind.ToString();
}

public static bool SetRegistryKeyAccessPermissions()
Expand All @@ -135,7 +135,7 @@
return false;
}

var user = Path.Combine(Environment.UserDomainName, Environment.UserName);
var user = $@"{Environment.UserDomainName}\{Environment.UserName}";
var registrySecurity = new RegistrySecurity();

var accessRule = new RegistryAccessRule(user,
Expand Down Expand Up @@ -180,10 +180,16 @@

try
{
var remoteBaseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, machineName);
using var remoteBaseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, machineName);

return true;
}
catch
catch (ArgumentException)
{
// An unreachable machine and a stopped Remote Registry service both surface here.
return false;
}
catch (IOException)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,54 @@
namespace Tests
{
// Every test in this class reads and writes the registry of the machine that runs it,
// so it can only pass on Windows. The names carry "Live" and CI excludes them with
// --filter "FullyQualifiedName!~Live". Run them locally on Windows.
[TestClass]
public class RegistryDemoTests
{
[TestMethod]
public void WhenGetCurrentUserRootKeyName_ResultIsRootKeyCurrentUserName()
public void WhenGetCurrentUserRootKeyName_ResultIsRootKeyCurrentUserName_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var currentUserRegistryName = RegistryDemo.GetCurrentUserRootKeyName();

Assert.AreEqual(currentUserRegistryName, Registry.CurrentUser.Name);

Check warning on line 14 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/Tests/RegistryDemoTests.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'Registry.CurrentUser' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 14 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/Tests/RegistryDemoTests.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'RegistryKey.Name' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}

[TestMethod]
public void WhenGetCurrentUserRootKeyNameWithPlatformCheck_ResultIsRootKeyCurrentUserName()
public void WhenGetCurrentUserRootKeyNameWithPlatformCheck_ResultIsRootKeyCurrentUserName_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var currentUserRegistryName = RegistryDemo.GetCurrentUserRootKeyNameWithPlatformCheck();

Assert.AreEqual(currentUserRegistryName, Registry.CurrentUser.Name);

Check warning on line 22 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/Tests/RegistryDemoTests.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'RegistryKey.Name' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 22 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/Tests/RegistryDemoTests.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

This call site is reachable on all platforms. 'Registry.CurrentUser' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}

[TestMethod]
public void WhenGetCurrentUserRootKeySubkeyCount_ResultIsGreaterThanZero()
public void WhenGetCurrentUserRootKeySubkeyCount_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var subKeyCount = RegistryDemo.GetCurrentUserRootKeySubkeyCount();

Assert.IsTrue(subKeyCount > 0);
}

[TestMethod]
public void WhenReadAndWriteRegistryValueUsingRegistryClass_ResultIsCodeMazeRegistryDemoValue()
public void WhenReadAndWriteRegistryValueUsingRegistryClass_ResultIsCodeMazeRegistryDemoValue_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var writtenValue = RegistryDemo.ReadAndWriteRegistryValueUsingRegistryClass();

Assert.AreEqual(writtenValue, RegistryDemo.CodeMazeRegistryDemoValue);

Check warning on line 38 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/Tests/RegistryDemoTests.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

Assertion arguments should be passed in the correct order. 'actual' and 'expected'/'notExpected' arguments have been swapped. (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0017)
}

[TestMethod]
public void WhenReadAndWriteRegistryValueUsingRegistryKeyClass_ResultIsCodeMazeRegistryDemoValue()
public void WhenReadAndWriteRegistryValueUsingRegistryKeyClass_ResultIsCodeMazeRegistryDemoValue_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var writtenValue = RegistryDemo.ReadAndWriteRegistryValueUsingRegistryKeyClass();

Assert.AreEqual(writtenValue, RegistryDemo.CodeMazeRegistryDemoValue);

Check warning on line 46 in dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp/Tests/RegistryDemoTests.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-platform-specific/ReadWriteWindowsRegistryInCSharp)

Assertion arguments should be passed in the correct order. 'actual' and 'expected'/'notExpected' arguments have been swapped. (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0017)
}

[TestMethod]
public void WhenGetSubKeyNames_ResultAreTwoSpecificNames()
public void WhenGetSubKeyNames_ResultAreTwoSpecificNames_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var subKeyNames = RegistryDemo.GetSubKeyNames();

Assert.IsTrue(subKeyNames.Length == 2);
Expand All @@ -84,13 +57,8 @@
}

[TestMethod]
public void WhenGetValueNames_ResultAreTwoSpecificValues()
public void WhenGetValueNames_ResultAreTwoSpecificValues_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var valueNames = RegistryDemo.GetValueNames();

Assert.IsTrue(valueNames.Length == 2);
Expand All @@ -99,42 +67,27 @@
}

[TestMethod]
public void WhenGetValueKind_ResultIsString()
public void WhenGetValueKind_ResultIsString_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var valueKind = RegistryDemo.GetValueKind();

Assert.IsTrue(valueKind.Equals("String"));
}

[TestMethod]
public void WhenSetRegistryKeyAccessPermissions_ResultIsTrue()
public void WhenSetRegistryKeyAccessPermissions_ResultIsTrue_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var setPermission = RegistryDemo.SetRegistryKeyAccessPermissions();

Assert.IsTrue(setPermission);
}

[TestMethod]
public void WhenOpenRemoteBaseKey_ResultIsFalse()
public void WhenOpenRemoteBaseKey_ResultIsFalse_Live()
{
if (!OperatingSystem.IsWindows())
{
return;
}

var openRemote = RegistryDemo.OpenRemoteBaseKey("machineName");

Assert.IsFalse(openRemote);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading