0

I'm writing an app that needs to work every time the computer starts after the user installs it. i tried to do it on the installer calss in the afterInstall event but the installer puts it self to the registry and runs when windows restarts, so I tried to do it with the commited event and got the same results. After that I chenged the commited property installer class to false but then the commited evet dont fire. My last try was to run the app after it installs and then let it write itself to the registry and a strange thing happened it did writh to the registry but no to the place I wanted it to be does anyone know why that is and how can I fix it?

My code:

bool registry = true;

RegistryKey rkSubKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", false);
string[] values = rkSubKey.GetValueNames();

foreach(string name in values)
{

  if (name.Equals("appName"))
    registry = false;
}

if (registry)
{

  RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  rkApp.SetValue("appName", Application.ExecutablePath.ToString());
  DialogResult r = MessageBox.Show("The system now needs to restart your computer whould you like to do it now?", "Restart is needed", MessageBoxButtons.YesNo);
  if (r == DialogResult.Yes)
  {
    System.Diagnostics.Process.Start("ShutDown", "/r");
  }
  return;
}
mainModule.start();
ymn
  • 2,175
  • 2
  • 21
  • 39
user1839169
  • 207
  • 2
  • 4
  • 16

1 Answers1

0

Have you tried to open the subroot key smth like this:

var HKLM = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser,   Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);

and then get your subkey:

var baseKey = HKLM.OpenSubKey(...<the path here>..)

?

Ritro
  • 148
  • 3
  • 10
  • it gets it to the all user dir in the registry but that ok thanks :) – user1839169 Feb 18 '13 at 11:38
  • but it still writes in the all user section insted of the current user – user1839169 Feb 18 '13 at 12:06
  • It writes to HKEY_CURRENT_USER\\ (in your case HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run). Where do you want it to write to? – Ritro Feb 18 '13 at 12:49
  • You can use LocalMachine hive for current user on that machine as well. Also look [this](http://stackoverflow.com/questions/10941338/why-is-registry-written-in-different-location-than-expected) question - it has very interesting suggestions on topic. – Ritro Feb 18 '13 at 13:00