In this we are going to see how we can create setup (MSI) file for an Windows application. Along with setup creation we are adding a custom control to msi file which facilitates to frame connection string during installation. Connection string details (Instance name, Database name) will take from user during installation process. For adding custom control to setup we need to design a page which facilitates to take all the inputs that are necessary to frame Connection string.
How It Is Useful
This custom controls enables the end user to make his own connection string during installation process.
- It eliminates the hard coding of connection string in
App.config file which makes an application more user friendly.
- We can divide this into two parts
- Creation of Setup file
- Adding Custom Control to Setup.
Creation of Setup file
- Step 1: Open or Create an Application, Add a new project by Right Click > Solution Explorer >Add >New Project > Other Project types>Setup and Deployment >Visual Studio Installer >Setup Project (Windows app) for Web App (Web setup Project).
- Step 2: Add Web.config or App.config and installer class (right Click >project>Add new Item >Installer class) to existing project.
3: Go to properties of Setup project and change Author name, Manufacturer, Product name, Title etc.,
- Step 4: Right Click on Setup project >Add >Project Output > Select primary out put and Content files too
- Step 5: Select the referring
DLLs of the application right click>properties>Set copy to local to True
protected override void OnAfterInstall(IDictionary savedState)
string dataSource = Context.Parameters["DataSource"];
dataSource = dataSource.Replace('/', ';');
MessageBox.Show("instance=" + dataSource);
dataSource = dataSource.Replace("$", @"\");
dataSource = dataSource.Replace("UserID", @"User ID");
dataSource = "Data source = " + dataSource.Replace("InitialCatalog",
string pwd = Context.Parameters["Password"];
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
string configFile = string.Concat(Assembly.GetExecutingAssembly().Location,
map.ExeConfigFilename = configFile;
System.Configuration.Configuration config = System.Configuration.ConfigurationManager
string connectionsection = config.ConnectionStrings.ConnectionStrings
//Removing Existing Connection string if available Adding new one
ConnectionStringSettings connectionstring = null;
if (connectionsection != null)
connectionstring = new ConnectionStringSettings("SqlConnectionString", dataSource);
SqlConnection con = new SqlConnection(connectionstring.ConnectionString);
Adding Custom Control
Designing Custom Control
We need to design our custom control according to our requirement. In this scenario we required Text boxes, we will have inbuilt text boxes, we need to add those to our Custom install wizard. We can make four text boxes which can be enabled and disable with this property "Edit Visible". We can also adjust the display of our designed control with respective to installation process.
UI design for Custom Control
Alignment order of Installation Process
Step 1: Right Click on Setup project > View >User Interface> It will shows the installation process steps >Right click on Start Node>Add Diloug>Select Text boxes>Change properties Accordingly.In properties it shows four Text boxes if we set property (Edit Visible)
to true it will be visible.
- Step 2: Right Click on Setup project > View >Custom Actions> Click on Install node>Add Custom Action>Click on to Application Folder>Choose Primary output file. Perform the same action for Commit, Rollback, uninstall nodes.
- Step 3: Go to properties of Primary output file of Install node, Custom Action data pass the input parameters of connection string as follows.
For this data should pass as <Key> and <value>. Text box label name should match to the key and property name matched to Value. We should provide custom action data to primary output file of install only.
- Step 4: Setup file will create once we Complete the Build and setup file was created in debug folder, install it and check the config file in installed location (C:/programming files /path name .....) you will get new connection string with user provided details.
- Custom Action Data Will take all input parameters as an single string.
- We need to provide input in (Custom Action Data) primary output file of install node only.
- Another way to pass parameters in custom Action Data is as follows:
/DataSource="[EDITA1]" /InitialCatalog="[EDITA2]" /UserID="[EDITA3]" /Password="[EDITA4]"
If we pass the custom action data parameter in this method we can get each one