Custom Installer

Brief:

Custom installer allows you to perform any custom action before install or after installation of the application.
We have created a custom installer for our one of the windows application where we have customized the traditional windows installer, in such way that we can create some directories at the time of installation, then we can change the file permission.
We also provide some change log file which we read while installation and add the changes to database. We also do some registry changes with this custom installer.

Need:


We create installers for ease of installation of our application for people without additional knowledge of technology. Visual Studio allows to create setup through setup project in very easy steps. But if we want to perform any additional work before or after the installation, then we can add custom actions.
For example, we want to add an admin user in the database just after installation, then we can implement a windows form with username, password, confirm password and save button. It can have logic to save the entered information in the database.
Similarly, we can add logic to do some pre-requisite check before starting the installation.


How It Works:


First, let’s create a simple one form windows application, with only a text box and a button.
Creating a windows application is just for the sake of having one to install.
I gave the name CreatingInstaller to my windows application, obviously you can choose your own.
Adding a new Windows Form Application in my solution and adding a text box and button to the default form resulted in the figure as shown below. Decorate the control properties however you want.
Just wanted to write few lines of code, so I binded the button’s click event to show text box's text.
So far so good. Now let’s create an installer for the same windows application. Right click on the solution and add a new project to your solution.
And add a setup project by Other project Types->Setup and Deployment->Visual Studio Installer
The project will be added to the solution. Now open the file system editor by clicking on the project and select the option to open file system editor.
You'll get to see Application Folder, User’s Desktop and User’s Program Menu.
Right click on Application Folder and add an output project. Out project specifies the project we are creating an installer to
Select CreatingInstaller (i.e. the windows application project name) in the add output project window and select it as a primary output and click OK.
The Primary output will be added as shown below, having type defined as Output.
right click on Application Folder in File system editor and add a folder. The folder will be created just below the Application Folder, name that folder Input.
Right-click on folder, select properties, and mark the Always Create property to True. That means the folder will always be created whenever we run the installer, after a fresh build release.


You can decorate your form to add an icon to it, and that icon will also be required at the time of installation to create a shotcut icon to our application. Add an icon to the form
To create a shortcut to the application, right click on Primary output in middle window pane and select Create shortcut to Primary output, name that shortcut as CreatingInstaller.
Select the properties of the shortcut, by right clicking it and add an icon to it. This icon will be created on the desktop when the application launches.
Cut the shortcut created at Application Folder and Paste it under User’s Desktop Folder.
For shortcuts to be created at the User’s Program Menu, add a new folder to the User’s Program Menu. This will be created at the program’s menu location in that folder. Create a new shortcut pointing to the primary output as we did when we created a desktop shortcut.
Name the folder CreatingInstaller.
Right click on middle window pane to create a new shortcut.
Select shortcut source to primary output selected.
Also add icon to shortcut, as done for Desktop shortcut.
Right click Application folder to set the properties of where to install the application.
Save all and Rebuild the setup project.
Job Done!
Now our setup is ready to install our windows application.
Just browse the debug folder location of Setup project. We find an msi and a setup.exe. You can run either to initiate setup.
When we started we saw a setup wizard with screens that welcomed the user, asked for the location to install (while the default location was already set.)
After completing the wizard, click the close button.
Now that the job is done we can see our shortcuts to the application created at desktop and User’s Program Menu
Just wanted to give a glimpse of Custom Actions we can define, while creating the setup.
Just add an installer class to the windows application we created earlier. When we open the installer class we can see the events specified for each custom action i.e. for Installation, Uninstallation, Rollback, Commit.
The code contains the logic to find the running EXE name at the time of uninstallation. If it matches my application EXE name, it kills the process. Not going into more details to it.Just want to explain the use of custom actions.

public override void Uninstall(IDictionary savedState)
{
  Process application = null;
   foreach (var process in Process.GetProcesses())  

   { 
    if(!process.ProcessName.ToLower().Contains("creatinginstaller"))
    continue;

    application = process;
    break;
   }


        if (application != null && application.Responding)
   {
     application.Kill();
     base.Uninstall(savedState);
   }
}


Click on the Custom Actions Editor after selecting the CreatingInstallerSetup project.
We see the custom action editor pane on left window. Right click it to add a custom action and select the primary output in the Application Folder.
We see primary output added as custom actions now. at the time of uninstallation my custom action will be fired and the application will be closed while uninstalling it.
Now when we browse the Debug folder of the Setup project we see two more folders as a result of the actions we performed just now.
Now this whole package has to be supplied to the client machine for the installation of the application.
Now re-install the application from setup.exe, and launch it using shortcuts.

Boston Byte Grabs a Spot in Clutch’s List of Top Software Developers in Massachusetts

Boston Byte is a collective of highly-skilled and highly-professional developers dedicated to solving your technological ...