Search code examples
c#windowregistry

c# add program to windows startup (windows 7)


I'm trying to add my program to the windows start up programs by adding the program execution path to the registry editor.

This is the code:

RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);  
registryKey.SetValue("progExe", Application.ExecutablePath);

Surprisingly, when i restart the computer, the program does not run in the startup...

I can see the the program is the msconfig window ,with it's checkbox ticked.. but when i go that registry path,i can't find the key i used at all("progEXE")...i made sure i'm searching in the right registry path...

enter image description here

enter image description here Does anyone has any idea what could be the problem in this case?

Thanks in advance.


Solution

  • Off-topic: add a null check before trying to set the value, or write with a questionmark like so "registryKey?.SetValue(..."