If you have ever designed Windows applications, you must have faced the big challenge of delivering software to customers. This problem is more apparent when the product is distributed among a large number of customers and you can't actually be present at the installation site of the program. Application dependencies on .NET Framework, components, and database services (often SQL Server) can present significant challenges for correct implementation on the host system.

Understanding Software Deployment

After completing the software production process and performing all tests, during the set of activities called Software Deployment, the software is made available to users for final use. One of these activities is Setup. The Setup program performs a set of operations on the host system, including:

  • Copying application files including executable files, libraries, resources, etc. to the host system
  • Registration of required values such as installation path in the host system registry
  • File format registration used by the program on the host system
  • Installation of dependencies needed to run the program
  • Creation of necessary shortcuts on the host system to run the program and access files

To create Setup files, there are many programs available, with InstallShield being one of the most popular. The latest version is available as a template in Visual Studio 2010's Setup and Deployment section. If you plan to take advantage of all the InstallShield features, you'll need to purchase the full version.

Another solution is to create Setup through Visual Studio's built-in features. Visual Studio offers a Setup project in the Setup and Deployment section. With this project type, we can meet most software deployment process needs. The output is a Setup file that the end user can use to install the application on their system. In this article, we'll explore the most important aspects of creating a Setup project.

Choose Your .NET Framework Version Carefully!

One of the most important points that new developers should pay attention to is the version of .NET Framework they use to implement their applications. If the version of .NET Framework with which the application was developed hasn't been installed on the host system, the program will not run.

Microsoft Corporation, after Windows Vista, established .NET Framework as one of Windows' core components. This means that with Windows installation, all .NET Framework versions supported at the time of the Windows release will be installed. However, considering that a wide range of users in many countries still use Windows XP (and even older versions!), this must be taken into full consideration.

It's recommended that all application requirements be reviewed during the design phase, and the lowest version of .NET Framework necessary for implementation be chosen. For example, if you don't plan to use WPF, WCF libraries, or other newer features, .NET Framework 2.0 will meet your needs. Many programmers only discover this issue during deployment, when fixing it can be time-consuming or even impossible in some cases. Choosing the right version also affects the size of your Setup package.

Creating a Setup Project

Follow this path to create a Setup project:

File > New > Project... > Other Project Types > Setup and Deployment > Setup Project

A Setup project provides several sections:

  • File System: Makes the host file system available. All files that must be copied to the host system are specified here.
  • Registry: Through this section, desired keys can be added to the host system registry.
  • File Types: Allows registration of new file structures in the host system.
  • User Interface: Offers the installation program interface. You can add and move dialogs according to your needs.
  • Custom Actions: If specific actions need to be performed during installation or removal, they can be defined here.
  • Launch Conditions: Installation conditions, such as checking for specific files or permissions, can be defined here.

Step 1: Adding Files to the Setup

In the first step, go to the File System section to add the files that Setup needs to copy to the host system. File System by default consists of three main folders:

  • Application Folder: All files placed here will be copied to the program folder (typically in Program Files).
  • User's Desktop: Provides the ability to create shortcuts on the end user's desktop.
  • User's Programs Menu: Allows adding program folders and shortcuts to the Start menu.

All output files of a project can be added to Setup by adding Project Output. Select the project and file type you want to add. This can be repeated for other projects or file types. After that, you can add program shortcuts to the desktop and program menu by selecting the folder and using the "Create Shortcut" option from the right-click menu.

Step 2: Customizing the User Interface

After adding all files and resources in the File System section, you can adjust the Setup dialogs and interface. Go to the User Interface section and add or move dialogs through the right-click menu. Setup has two execution modes (User and Administrative), each with its own dialogs and interface.

In most cases, you'll need to collect information from the user during installation. Various dialogs are provided for different types of information (boolean, string, etc.). Choose the most suitable dialog for your needs and customize it through the Properties window.

The Splash dialog type is intended to display an image at the beginning of the Setup Wizard. Its image size is 317×477 pixels. All dialogs also have a BannerBitmap to display at the top of the form (68×496 pixels). If you plan to use custom images, add them to the Application Folder in the File System section first.

Step 3: Identifying Application Requirements

At this point, identify the requirements for your application to run. As mentioned earlier, the correct choice of .NET Framework version will affect the installation process. To determine the .NET Framework requirements, select "Launch Condition" from the Project menu, then add a new condition.

After determining the .NET Framework version, go to Project > Properties and click on "Prerequisites". Here, you can specify all application requirements and have Setup install them automatically if they're not present on the host system.

All popular components are listed in this dialog. Selecting the "Download prerequisites from the same location as my application" option will include all these components alongside your Setup file after building. Note that along with the original Setup file, another setup file will be generated for installing prerequisites. This file must be executed during installation to check and install any missing requirements before the main Setup runs automatically.

Note that Setup itself doesn't depend on .NET Framework. However, Windows Installer 3.1 must be installed on the host system to run it. This library is usually installed with Windows, but in cases where it isn't, it will need to be installed before Setup can run.

The Database Challenge: SQL Server Integration

One of the biggest challenges in software deployment is often installing SQL Server and adding databases to it. If your application uses a database to store information, you'll need to install SQL Server on the host system and attach the database.

The Custom Actions section is designed precisely for scenarios like this. Custom Actions can be defined for any of these installation phases:

  • Install: The main installation phase
  • Commit: Confirmation step after installation
  • Rollback: Step to undo installation operations if something fails
  • Uninstall: System removal phase

For database integration, you can create a Class Library project with an Installer class and add it to your Setup project. This class can perform operations like attaching a database during installation:

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Data.SqlClient;
using System.IO;
 
namespace DbLib
{
    [RunInstaller(true)]
    public partial class DbInstaller : Installer
    {
        public DbInstaller()
        {
            InitializeComponent();
        }
 
        // This function will be called during installation
        public override void Install(IDictionary stateSaver)
        {
            // Installation path
            var installDir = Context.Parameters["InstallDir"].Trim();
            // Database path
            var DbDirectory = Path.Combine(installDir, "Data Base\\PdDataBase.mdf");
            // Connection to server
            var pDConnection = new SqlConnection(@"Server=.\SQLExpress;Integrated security=SSPI;database=master");
            // SQL script for attaching the database
            var commandString = string.Format("CREATE DATABASE PdDataBase ON PRIMARY (FILENAME = '{0}') FOR ATTACH",DbDirectory);
            // Command for attaching the database
            var pDCommnad = new SqlCommand(commandString, pDConnection);
            try
            {
                // Open connection
                pDConnection.Open();
                // Execute command to attach database
                pDCommnad.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
               // Error handling code here
            }
            finally
            {
                if (pDConnection.State == ConnectionState.Open)
                {
                    // Close connection
                    pDConnection.Close();
                }
            }
 
            base.Install(stateSaver);
        }
    }
}

There are typically two basic database scenarios:

  1. The database must be created during installation (when no initial data exists)
  2. The database must be attached during installation (usually when initial data is needed)

The second scenario can be implemented as an extension of the first by creating the database and then inserting the required data. In Vista and Windows 7 operating systems, due to security issues, it's better to run the installation program with administrator privileges.

Final Step: Checking Setup

As a final step, check your Setup project. The uninstall program is automatically registered in the list of installed programs on the target system, allowing users to remove it at any time. You can also select an icon for the uninstall program in the project properties.

Conclusion

The scenario covered in this article is the most common setup scenario, implemented with Visual Studio 2008. Different needs in other scenarios can be addressed using Setup project facilities. For example, installing a Windows service is another common scenario.

This article was based on frequently asked questions about Setup in our website forum. We hope it has covered the needs in this area. Please share your views and suggestions for future articles in this series.

Any use of exclusive PersiaDevelopers articles is permitted subject to citation of the source and author name.