Wednesday, August 13, 2008

Installing a Startup Registry Key in a Setup Projects

Here are the steps to install a startup registry key in a setup project in Visual Studio 2008.

1. Create a setup project.

2. Right click on the setup project, go to "View" and then "Registry":

3. Right click on the folders under HKEY_CURRENT_USER and select "New" and then "Key". You will need to add the folders in the following hierarchy:


Some of the hierarchy may have already been created for you. If so, just start building your hierarchy off of that.

4. Next, you'll need to add the registry value. Right click on the "Run" folder (above), and choose "New" followed by "String Value". You should see a new value pop up in the pane on the right.

5. Rename the value on the pane on the right whatever you want to name it. It's customary to give it a name that fits with your application, and it's fine to just put your application name as the name of the value.

6. Set the Value property of your value. Right click on the value you just added, and select "Properties Window". In the window, you'll see a field called "Value". This is where we want to put in the path of the application to execute. Here's the catch: the default setup project allows the user to specify where they would like to install the application, so this path can be variable. We can capture the location where the application has been installed to by calling the "TARGETDIR" variable. This variable has to be put in square brackets for the installer to recognize that it's a dynamic value.

For example, if I want the application, named "MyApplication.exe" to run, I need to make the value of the key the following:


When the installer is running, it will replace "[TARGETDIR]" with the directory to which the file was installed. So if the user installs the application to "C:\Program Files\MyCompany\MyApplication\", the value that will be put in this registry key will be "C:\Program Files\MyCompany\MyApplication\MyApplication.exe".

That's it! Now run the installer, reboot, and your application should run!