How to read and overwrite the Shell in windows using C# Windows Forms

Edit: I didn’t want to delete my post fully so edited it. I have found this code snippet:

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRunsomeKey", true);

//getting the value
string data = key.GetValue("someValue").ToString();  //returns the text found in 'someValue'

key.Close();

The code snippet is from here and I cant seem to figure out how to replace the shell or even read from the string, can someone help me fix my mistake?

My code:

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\WinLogon\Shell", true);

            //getting the value
            string data = key.GetValue("Shell").ToString();  //returns the text found in 'someValue'

            key.Close();
            MessageBox.Show(data);

Errors during runtime:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

What I have tried:

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\WinLogon", true);

            //getting the value
            string data = key.GetValue("Shell").ToString();  //returns the text found in 'someValue'

            key.Close();
            MessageBox.Show(data);
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\WinLogon", true);

            //getting the value
            string data = key.GetValue("Shell").ToString();  //returns the text found in 'someValue'
MessageBox.Show(data);

            key.Close();

I have also removed MessageBox.Show(data); from the code yet it does not want to work. The same goes for overwriting the shell.

Edit 2: I have tried changing LocalMachine to CurrentUser, it still did not work. I have tested with admin and without admin privileges.

Please help Thx. Most comments are from the old question which has shifted into this.

Answer

The solution to this problem was not in changing to CurrentUser from LocalMachine, LocalMachine is correct as that is where the shell data/shell value is stored, there is no path for it under CurrentUser.

In your code, you either have made a mistake while importing the needed using Microsoft.Win32; or you just have it down wrong.

Your code to solve the problem should look like this:

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\WinLogon", true);

            //getting the value
            string data = key.GetValue("Shell").ToString();  //returns the text found in 'Shell'

            key.Close();
            MessageBox.Show(data);

Keep in mind that for administrator privileges you need the app.manifest created and or updated to <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> from <requestedExecutionLevel level="asInvoker" uiAccess="false" />

With these changes, the code should work with no problem.

Tested on a default WindowsForms C# Application with the app.manifest changed to have administrator privileges for the app. Visual Studio 2019 latest version with the .NET Framework 4.7.2 set as the target framework.

If you need any other help please share your Visual Studio version, the framework you are using and any other information that you may have on hand.