Custom Actions are installation components that will be invoked during the setup process. To view the Custom Actions editor, right-click on the setup project then click view --> Custom Actions as seen in Figure 12. By looking at figure 12, we can see that Custom Actions can be invoked at any four specific points: Install, Commit, Rollback and Uninstall. To satisfy our objectives I needed to set up some files and folders during the install step. If the specified folders are already in place I do not recreate them, but I do recreate files.
Figure 12: The Custom Action Editor
Creating a Custom Actions program is straightforward. We create a new Class Library project, SetupCustomActions that manipulates files/folders on the target machine during the installation. To do so, we created a project with the New Project Wizard, specifying Class Library as the template. By default, a class called Class1 is created in the project. Delete this class from the project and use the Add New Item wizard to add an Installer Class to the project, as shown in Figure 13.
Figure 13: Adding an Installer Class to a custom action project
Now, we are ready to write our code. As seen in the Figure 14.
Figure 14: Setting up an installer class.
It is important to notice that the SetupCustomAction class inherits the System.Configuration.Install.Installer and RunInstaller attribute is true [RunInstaller(true)]. Otherwise the SetupCustomAction will not be invoked by the setup program.
To add code to the SetupCustomAction Class we first have to override the Install method.
public override void Install(IDictionary stateSaver)
{
base.Install (stateSaver);
string companyName = Context.Parameters["cName"];
CreateFilesFolders(companyName);
}
Then we create a configuration file. See the code for implementation.
At this point, compile the SetupCustomAction using right-click on SetupCustomAction project then select Build. Now, we are ready to add our compiled class (specifically .dll) file to the setup project. To do so right-click the setup project Add --> File. Select the SetupCustomAction.dll from the Add Files dialog box (You may have to browse to the appropriate folder). The SetupCustomAction.dll will appear under the setup project.
After adding the .dll to the Setup project, open the Custom Actions Editor by right-clicking on the Setup project in the Solution Explorer and choose View --> Custom Actions in the pop-up menu. In the Custom Actions Editor, right-click, on the Install folder and select the Add Custom Action... command from the pop-up menu then double-click on Applications folder on Select Item in Project dialog box and select SetupCustomAction.dll, then click OK, as seen in Figure 15.
Figure 15: The SetupCustomAction.dll in the Select Item in Project dialog box
Furthermore, we need to introduce the SetupCustomAction.dll to the setup project so that this component can be invoked during the installation process. We also have to link the user input to a variable that can be accessed from code. This is done by setting some properties of SetupCustomAction.dll, as shown in Figure 16.
Figure 16: The properties of SetupCustomAction.dll under Custom Actions.
The property CustomActionData associates a variable named cName with the COMPANYNAME_A1 input field. Once we have this association, we obtain the value entered using the following line of code.
string companyName = Context.Parameters["cName"];
Note: The CustomActionData takes a format of /name=value. Multiple values must be separated by a single space (i.e. /name1=value1 /name2=value2). Also, if the value has a space in it, it must be surrounded by quotes.
Now, we're ready to write a method where we create an XML file. We record user's information in the XML file and read back from it at run time to provide a customized interface. At this point, recompile the setup program so it will be ready for distribution.