Jens
Jens

Reputation: 89

Why can't I read this registry key in C#

Problem

Using Code:

Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SystemProtectedUserData\S-1-5-21-436374069-1965331169-839522115-50811\AnyoneRead\LockScreen", "", null);

Diagnostics of problem

I am guessing this is some permission problem but I dont know how to fix it.

Any help is appreciated.

Upvotes: 0

Views: 531

Answers (2)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23308

Your Registry.GetValue method returns null, because it can't find a specified key or value and the default value (the third argument) is set to null. According to documentation, this method can't throw a NullReferenceException, it seems the consuming code causes it.

So, you can try to specify the value name (at my end I see only OSVersion value) or different default value.

If the issue still exist, the problem can be in redirection to WOW6432Node registry node in x64 edition of Windows. You can avoid it by the following code, using OpenBaseKey method and specifying a RegistryView value

var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\SystemProtectedUserData\S-1-5-21-...\AnyoneRead\LockScreen");
var value = key.GetValue("OSVersion"); //for example

Upvotes: 3

Alb
Alb

Reputation: 529

You didn't define the valueName thus it returns the default value, which you set to null.

Check the documentation. (More specifically, you defined it to string.Empty which is not a valid name. Maybe LockScreen is the name of the value/registry-variable? - I don't speak android)

Upvotes: 0

Related Questions