I'm running my app on Windows 8.1 (64-bit), and I want to create a value in HKLM\Software\Microsoft\Windows\Current Version\Run
This is my code:
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);
}
This never enters the catch block, so there is no exception, but the key is never created.
I have already tried the microsoft approach:
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();
But get the same result. Can someone can tell me why is this happening and/or how can i make it work?
Just in case you're still looking for an answer: Do you have sufficient rights to write into HKLM? Else, the entry might enter the VirtualStore without you telling. You can search for your value in the registry as @PetSerAl suggested.