Why is the Registry Key showing null in C# even if it is present in Registry Editor?

Yuvaan Nevatia 1 Reputation point
2022-10-27T11:25:10.407+00:00

Here is my C# code:

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile", true))
{
key.CreateSubKey("NetworkThrottlingIndex");
key.SetValue("NetworkThrottlingIndex", "0000000a", RegistryValueKind.DWord);
key.CreateSubKey("SystemResponsiveness");
key.SetValue("SystemResponsiveness", "00000000", RegistryValueKind.DWord);
key.CreateSubKey("NoLazyMode");
key.SetValue("NoLazyMode", "1");
}

So what is happening here is that I am getting an error

System.NullReferenceException  

on the line

key.CreateSubKey("NetworkThrottlingIndex");  

because the key is null.

I checked several times but this key is present in my registry editor.

Any idea about what is wrong here?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,640 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RLWA32 43,146 Reputation points
    2022-10-27T12:12:53.247+00:00

    Are you running a 32-bit process? On my system the key exists in the 64-bit view of the registry.

    0 comments No comments

  2. vcrobe 1 Reputation point
    2022-10-27T20:03:46.963+00:00

    I think that key is null for one of two reasons:

    1. The key was not found so OpenSubKey() will return null
    2. You don't have permissions to open that key in write mode
    0 comments No comments

  3. Castorix31 83,201 Reputation points
    2022-10-27T20:19:30.083+00:00

    Try RegistryView.Registry64 like the test I had posted in this thread

    0 comments No comments