Reputation: 18202
I have checked some other examples.
I am working on a WinForm. & now i am going to create its installer.
i want to start the software when windows starts & trying to use the following code.
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
But i am confused that where to put this code. i don't want this code executed every time when my application starts as it will executed once when the software is installed.
shall i put check on form_Load() if the regkey is absent, & if yes then add this value to the Run.
is it correct ? i don't want to give an option to the user & i want to start this application at the startup compulsorily.
Thanks.
Upvotes: 0
Views: 1159
Reputation: 3751
You can create your customAction class for your installer like this: http://msdn.microsoft.com/en-us/library/d9k65z2d.aspx#Y0
Once you have this class, put your registry code in the commit section. Now, whenever your application will be installed, your application will be added to registry to run at startup.
Upvotes: 1
Reputation: 21898
Your installer should create the registry keys.
In addition, your installer may also ask whether to install for the current user or for all users. If user says everyone, then the key must be added in HKEY_LOCAL_MACHINE
rather than HKEY_CURRENT_USER
, which requires admin privileges. Installers do usually have that privilege .
Upvotes: 0
Reputation: 915
As the others users have commented, this kind of thing should go on the installer. On the setup project you can create with Visual Studio, you can add a link to the user's startup folder that should do the trick.
BUT
If you don't want the user tampering with the startup folder and you absolutely want the program to start automagically with Windows, you could do as you've said: check for the adequate registry key everytime the program starts, and if it's not there write it. Take into account your program will need elevated account rights for this.
BUT
Do you ABSOLUTELY need this? Are you absolutely sure you can't offer it as an option to the final user? If I was a user to your program, didn't want it on startup, take the effort to remove a registry key to get it out of there and then find out it's again in the registry without my consent, I'd be pretty pissed...
Upvotes: 1
Reputation: 172468
It depends on the istaller software that you use. If you use the setup project template provided by Visual Studio, you can have the registry key created automatically.
Upvotes: 0
Reputation: 5468
Make a small console application that uses the line and run that at the end of your installation.
Upvotes: 0