0

I'm not sure why this is throwing an exception... The exception isn't very specific.. Am I missing something? :(

I'm essentially trying to update a registry value on a remote machine. On the RegistryKey Key line, an exception is thrown: Object reference not set to an instance of an object. This is because the key is getting set to null. However, if I shorten the OpenSubKey value to just OpenSubKey("software",true); it works just fine. But I need to get two subkeys deeper than Software. Is there a limitation on the OpenSubKey class when it's used with the OpenRemoteBaseKey class?

ServiceController service = new ServiceController();
        service.MachineName = store;
        try
        {
            //Enable Remote registry
            string commandStart = @"/k sc \\" + store + " config remoteregistry start= auto && exit";
            string commandStop = @"/k sc \\" + store + " config remoteregistry start= disabled && exit";
            Process.Start("cmd.exe", commandStart);
            System.Threading.Thread.Sleep(2000);
            service.ServiceName = "remoteregistry";
            service.Start();

            //Disable User Input
            RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, store).OpenSubKey(@"software\realvnc\vncserver",true);
            key.SetValue("DisableLocalInputs", "1");
            CheckIsDisabled(key);

            //Re-enable User Input
            key.SetValue("DisableLocalInputs", "0");
            service.Stop();
            System.Threading.Thread.Sleep(2000);
            Process.Start("cmd.exe", commandStop);
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message,"Exception");
        }
Michael
  • 13
  • 3
  • OpenSubKey() returns null when it failed. The NRE is the inevitable outcome when you don't use a debugger and don't check for it. Exceptions are your friend, they tell you when you write bad code. – Hans Passant Aug 29 '17 at 23:02
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dymanoid Aug 30 '17 at 07:30
  • Are their limitations to appending more than one subkey to the OpenSubkey(string) when using OpenRemoteBaseKey? If I drop the string to just **.OpenSubKey(@"software",true);, I'm returned with the correct results. – Michael Aug 30 '17 at 15:37

0 Answers0