Stuart
Stuart

Reputation: 4258

.net standard, windows compatibility pack and registry

I have a .net standard 2.0 library that I want to use to access the registry via the windows compatibility pack (if the OS its running on is Windows)

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
     Console.WriteLine("On windows.");
     var dropFolder = Microsoft.Win32.Registry.GetValue(keyName: $@"{userRoot}\{subkey}", valueName: "DropFolder", defaultValue: @"C:\somepath");
     Console.WriteLine($"Drop folder is {dropFolder}");
 }

This crashes with an exception

Managed Debugging Assistant 'BindingFailure' : 'The assembly with display name 'Microsoft.Win32.Registry' failed to load in the 'Load' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Win32.Registry, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

I have installed the Windows Compatibility nuget package into my .net standard project. I have also tried installing the Microsoft.Win32.Registry nuget package.

The error perists.

If I comment out the lines

var dropFolder = Microsoft.Win32.Registry.GetValue(keyName: $@"{userRoot}\{subkey}", valueName: "DropFolder", defaultValue: @"C:\somepath");
Console.WriteLine($"Drop folder is {dropFolder}");

It works fine.

I am referencing the .net standard library from a .net framework library so no option to go to .net core.

The .dot net standard cs proj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0" />
    <PackageReference Include="MQTTnet" Version="2.8.2" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
    <PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
  </ItemGroup>

</Project>

Upvotes: 1

Views: 1792

Answers (1)

mm8
mm8

Reputation: 169200

For the missing binary to be included in the output of your .NET Framework project, you should also install the Microsoft.Windows.Compatibility package in this project. Or at least install the Microsoft.Win32.Registry package.

Upvotes: 2

Related Questions