Today I will explain Setup Projects and how to create a custom setup that can change our app.config.
1: Installing our environment
I will use Visual Studio 2013 and we need to install the following tool:
Microsoft Visual Studio 2013 Installer Projects:
- Download here.
- Follow the installation (next, next, next).
2: Creating a simple setup project
Before we start, I created a solution with the following two projects:
- Console Application (the application that we want to install);
- Setup Project (Setup to install our application)
My console application has the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ApplicationToInstall
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hellow World!!!");
- Console.ReadKey();
- }
- }
- }
Now we need to create our setup project with the following procedure:
- Solution Explorer, then click on Add and select New Project.
- Add a Setup Wizard.
- If you want to add some icon file, I suggest you include in the next form:
- When the wizard ends (finish), the following tab will open:
- On “application folder”, right-click “Primary Output from ApplicationToInstall (Active)” and select “Create Shortcut to Primary”.
- Drag and Drop the shortcut to “User's Desktop”:
- If you want to set an icon, press “F4” at shortcut and change the icon property.
- Build our setup project:
- Now let's install:
- At the end of installation, the shortcut will be at our desktop, if we open:
3: Incrementing functionalities
Now we will make some increments at our setup, right-click at Setup Project and go to “View" and select "User Interface”:
We can insert steps by right-click and Add Dialog:
For this article we will add TextBoxes:
Open the Property window (F4):
Now we can change any of these values, I will configure it as follows:
I will mark the textboxes that I don't want to show to false (visible as false). Let's test our setup, just rebuild and try to install:
It worked, but it doesn't do anything. Now we need to pass this parameter to some code and use them to do something. To do that, we will need to create an Installer Class for our main application (in my case, console application).
Go to Solution Explorer, select your main application, then click Add and select New Item:
And I will write the following code:
This code overrides the commit event to read our TESTPARAMETER (TextBox value) and then insert on app.config.
Before running the installation we need to link our TextBox with our installer class. Go to the setup project and open the “Custom Actions”:
As we override the commit method we need to insert a custom action on the commit step:
Click twice the “Application Folder”:
Press “F4” or go to properties:
At “CustomActionData” use the following value:
For our application:
/TESTPARAMETER=”[TESTPARAMETER]”
If you need more than one parameter just put like as follow:
/TESTPARAMETER=”[TESTPARAMETER]” /TESTPARAMETER2=”[TESTPARAMETER2]”
Now we can test our application, our current config will look like the following:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
- </startup>
- <appSettings>
- <add key="TestParameter" value="ThisValueHasToChange" />
- </appSettings>
- </configuration>
Let's run our setup (rebuild/install) and change the parameter:
Open the config file at the install folder:
That's it guys, enjoy.
4: Errors that can happen
If you receive the following error:
Put a custom action at the “Install” step (no need to configure, it can be empty).