RegistryKey.OpenRemoteBaseKey - 对象引用未设置为对象的实例?

时间:2017-08-29 22:46:28

标签: c# .net wpf registry

我不确定为什么这会引发异常......例外并不是非常具体......我错过了什么? :(

我实际上是在尝试更新远程计算机上的注册表值。在RegistryKey Key行,会抛出异常:Object reference not set to an instance of an object.这是因为key设置为null。但是,如果我将OpenSubKey值缩短为OpenSubKey("software",true);,它就可以了。但我需要得到比 Software 更深的两个子密钥。当OpenSubKey类与OpenRemoteBaseKey类一起使用时,它是否有限制?

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");
        }

0 个答案:

没有答案