我在Windows 8.1(64位)上运行我的应用,我想在其中创建一个值 HKLM\Software\Microsoft\Windows\Current Version\Run
这是我的代码:
try
{
// Setting
RegistryKey rk = Registry.LocalMachine;
// I have to use CreateSubKey
// (create or open it if already exits),
// 'cause OpenSubKey open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);
// Save the value
sk1.SetValue("Servicio de Respaldo de Base de Datos", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost", ""));
}
catch
{
MessageBox.Show("No se pudo Asignar el Inicio Automatico del servicio", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
这永远不会进入catch块,因此也不例外,但是永远不会创建密钥。
我已经尝试了微软的方法:
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
key.SetValue("Servicio de Respaldo de Base de Datos", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost", ""));
key.Close();
但是得到相同的结果。有人可以告诉我为什么会发生这种情况和/或如何使它起作用吗?
万一您仍在寻找答案:您是否有足够的权利写给HKLM?否则,该条目可能会在您不通知的情况下进入VirtualStore。您可以按照@PetSerAl的建议在注册表中搜索您的值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句