我不确定为什么这会引发异常......例外并不是非常具体......我错过了什么? :(
我实际上是在尝试更新远程计算机上的注册表值。在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");
}