What's the best way to programmatically start an application on login for Windows? I know you can do it by adding an item to the startup folder in the start menu, but I want to have an option in my application to turn it off and on.
+7
A:
This is how you could do it in C#:
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
"MyStartUp",
@"C:\StartUpApp.exe");
You basically need to add a registry entry. The above one works on start-up. There are a few more. I recommend that you download a tool like Autoruns to see all possible locations.
kgiannakakis
2008-12-22 08:37:08
Cool, i should be able to do that in c++ using windows functions. Thanks
Lodle
2008-12-22 08:42:23
Beware: the above code adds the autostart entry for all users. Also, you need to have admin rights to write to HKLM. I'd recommend changing "HKEY_LOCAL_MACHINE" to "HKEY_CURRENT_USER"
Piskvor
2008-12-22 09:02:04
I was just about to say the same thing. Piskvor++, once again wishing we could upvote comments.
Matthew Scharley
2008-12-22 09:09:42
Oh, you can also use the RunOnce key instead of the Run key to run things once (as the name would suggest) instead of all the time, if that's what you need.
Matthew Scharley
2008-12-22 09:12:06
+1
A:
How about installing your program as a Windows service? Services can be switched between 'disabled', 'manual' and 'automatic', and you can access services from within your code (even from a Java application) and manipulate its state.
Just a thought.
Yuval =8-)
Yuval
2008-12-22 08:46:06