How to access the class RegistryKey?

1.1k Views Asked by At

In the last few days, I tried to access regedit using c#.

The class RegistryKey is not defined after I added using Microsoft.Win32.

Can you help me?

Code:

using System;
using Microsoft.Win32;
using System.Security.Permissions;

namespace TMREAddons
{
    public class RegEdit
    {
        RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net")
    }
}

This is a basic class library, nothing more than that.

The targetFramework is:

<TargetFramework>netstandard2.1</TargetFramework>
1

There are 1 best solutions below

0
On BEST ANSWER

You're targeting netstandard2.1 - which basically means "I want this to be able to run on any platform that's compatible with .NET Standard 2.1". Given that the Windows Registry doesn't exist on every .NET Standard 2.1 compatible platform, I'm not surprised at the error you're getting. (In general, I wouldn't expect anything from Microsoft.Win32 to be supported in .NET Standard.)

You can add the Microsoft.Win32.Registry NuGet package as a dependency, and that should solve the compilation issue - although it will still fail if you run on a non-Windows platform, of course.