question

JansenBarbara-2072 avatar image
1 Vote"
JansenBarbara-2072 asked hasanylmaz-9608 edited

Accessing registry through a dll project - error The type name 'RegistryKey' could not be found in the namespace 'Microsoft.Win32'

Dear all,

When trying to call Registry.CurrentUser.OpenSubKey I am having The type name 'RegistryKey' could not be found in the namespace 'Microsoft.Win32'. This type has been forwarded to assembly 'Microsoft.Win32.Registry, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.

even if including "using Microsoft.Win32;".

Same code works within program.cs

Any reason for this please and how I can resolve

Thanks

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Adding Microsoft.Win32.Registry as a nuget package solves!

0 Votes 0 ·

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

The RegistryKey type is defined in the assembly Microsoft.Win32.Registry. You are referencing it from an older assembly that is forwarding to the newer assembly but that assembly isn't included. Add a reference to the Microsoft.Win32.Registry assembly and your problem should go away.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi and thanks for your feedback.

I did include a reference to Microsoft.Win32.Registry however it is stating Error CS1069 The type name 'Registry' could not be found in the namespace 'Microsoft.Win32'. This type has been forwarded to assembly 'Microsoft.Win32.Registry, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.

As said the same code works within program.cs. why it is differentiating between a normal c# app and a dll (class library)

Thanks

0 Votes 0 ·

Are you compiling your .NET app as a .NET Framework target and your class library as .NET Standard or .NET Core? If so then things are different. In the case of a .NET Framework app you'd add a reference using the standard Add Reference dialog. For .NET Standard/Core apps you'd have to include a NuGet package.

0 Votes 0 ·