How to read registry in .net 5
00:48 21 Apr 2026

I would like to be able to read a value from a registry key on windows within .net 5. All resources suggest to simply add using Microsoft.Win32; but I am unable to build.

When attempting to build the following:

using Microsoft.Win32;
using System;

namespace Praxis.FolderFinder
{
    internal class MyClass
    {
        private const string Key = @"HKEY_CURRENT_USER\path\to\my\key";

        static MyClass()
        {
            Console.WriteLine(Registry.GetValue(Key, "valueName", null));
        }
    }
}

I am getting the following build error:

PS D:\MG-ES> dotnet build
Restore succeeded with 1 warning(s) in 0.8s
    D:\MG-ES\folder.finder\Folder.finder.csproj : warning NU1510: PackageReference Microsoft.Win32.Registry will not be pruned. Consider removing this package from your dependencies, as it is likely unnecessary.
  Folder.finder net10.0 succeeded with 4 warning(s) (0.2s) → folder.finder\bin\Debug\net10.0\Folder.finder.dll
    D:\MG-ES\folder.finder\Folder.finder.csproj : warning NU1510: PackageReference Microsoft.Win32.Registry will not be pruned. Consider removing this package from your dependencies, as it is likely unnecessary.
    D:\MG-ES\folder.finder\FolderFinder.cs(40,36): warning CA1416: This call site is reachable on all platforms. 'FolderFinder.LoadLinuxVariables()' is only supported on: 'linux'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
    D:\MG-ES\folder.finder\FolderFinder.cs(39,39): warning CA1416: This call site is reachable on all platforms. 'FolderFinder.LoadWindowsVariables()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
    D:\MG-ES\folder.finder\Folders.cs(12,31): warning CA1416: This call site is reachable on all platforms. 'Registry.GetValue(string, string?, object?)' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
  Folder.finder net5.0 failed with 3 error(s) and 3 warning(s) (0.3s)
    D:\MG-ES\folder.finder\Folder.finder.csproj : warning NU1510: PackageReference Microsoft.Win32.Registry will not be pruned. Consider removing this package from your dependencies, as it is likely unnecessary.
    D:\MG-ES\folder.finder\Folders.cs(12,31): error CS0103: The name 'Registry' does not exist in the current context
    D:\MG-ES\folder.finder\FolderFinder.cs(96,38): error CS0103: The name 'Registry' does not exist in the current context
    D:\MG-ES\folder.finder\FolderFinder.cs(117,41): error CS0103: The name 'Registry' does not exist in the current context
    D:\MG-ES\folder.finder\FolderFinder.cs(39,39): warning CA1416: This call site is reachable on all platforms. 'FolderFinder.LoadWindowsVariables()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
    D:\MG-ES\folder.finder\FolderFinder.cs(40,36): warning CA1416: This call site is reachable on all platforms. 'FolderFinder.LoadLinuxVariables()' is only supported on: 'linux'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Build failed with 3 error(s) and 8 warning(s) in 1.4s

Please find the csproj below:



    
        net5.0;net10.0
        true
    


    
        D:\nuget\
    

    
        
        
        
        
        
    

    
        
        
    
    

Does anyone knowwhat I am doing wrong?

windows registry .net-5