SharePoint Pals
 | Sign In
Windows Application – Prepare an MSI which will update the Config File Values during Installation – Packaging and Deployment Project Type
by Sathish Nadarajan 12 Nov 2015
Author
SharePoint MVP
Visits   
Today  :   8     Total  :    4351

In one of the recent project requirement, we were creating a Windows Application. That needs to be packed as an MSI and the application requires some configuration values as well. In that case, we cannot ask every user to update their app.config file manually after the installation as this is an Windows Application. In that case, I wanted to create the MSI itself, to be capable to update the config files for us during the installation.

Let us see how to do that step by step. (It’s been a long time, we had gone through step by step right)

1. Create a Windows Application. Here, I have not done any other stuff. On the FormLoad, reading the config value and displays on the form. (For a better understanding, download the source code at the bottom of this article).

2. The solution looks like below.

clip_image002

3. Add a setup project.

clip_image004

4. Now, the solution will be as follows.

5. clip_image006

6. Add an Installer Class on the Windows Application.

clip_image008

7. On the Installer Class, write the following.

 [RunInstaller(true)]
     public partial class Installer : System.Configuration.Install.Installer
     {
         public override void Install(IDictionary stateSaver)
         {
             base.Install(stateSaver);
 
             MessageBox.Show(Context.Parameters.Count.ToString(), "Test Count");
             ////Installer.cs 
             string dataSource = "Data Source =" + Context.Parameters["DataSource"];
             string initialcatalog = "Initial Catalog=" + Context.Parameters["InitialCatalog"];
             dataSource = dataSource + ";" + initialcatalog;
             dataSource = dataSource + ";Integrated Security=SSPI;";
             MessageBox.Show("instance=" + dataSource);
             ExeConfigurationFileMap map = new ExeConfigurationFileMap();
             MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
             //Getting the path location 
             string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
             map.ExeConfigFilename = configFile;
             System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
             string connectionsection = config.ConnectionStrings.ConnectionStrings
             ["SqlConnectionString"].ConnectionString;
 
             ConnectionStringSettings connectionstring = null;
             if (connectionsection != null)
             {
                 config.ConnectionStrings.ConnectionStrings.Remove("SqlConnectionString");
                 MessageBox.Show("removing existing Connection String");
             }
 
             connectionstring = new ConnectionStringSettings("SqlConnectionString", dataSource);
             config.ConnectionStrings.ConnectionStrings.Add(connectionstring);
 
             config.Save(ConfigurationSaveMode.Modified, true);
             ConfigurationManager.RefreshSection("connectionStrings");
         }
         public Installer()
         {
             InitializeComponent();
 
             
         }
     }
 

8. Now, we are done with the Windows Application. The rest all steps are on the Setup project.

9. Double click on the Content Files and we will be seeing the three Folders.

a. Application Folder

b. User’s Desktop

c. User’s Program Menu.

clip_image010

10. Add the Output files accordingly. (As these are very basic things, am skipping these)

11. Right click on the Setup Project and select the Custom Actions.

clip_image012

12. On the Install, Select the Primary Output file and view the properties.

clip_image014

13. Edit the CustomActionData with the Value,

/DataSource=[EDITA1] /InitialCatalog=[EDITA2] /UserID=[EDITA3] /Password=[EDITA4]

14. Here the EDITA1, EDITA2 etc., were the TextBox Values on the Interface. Let us see how to add the interface now.

15. Again select the Setup project and click on the Views-User Interfaces.

clip_image016

16. The below screen will be displayed.

clip_image018

17. On the Start, Right click and add dialog.

clip_image020

18. Select a Dialog with 4 Text Boxes.

19. Edit/Verify the Properties of the Dialog Properties.

clip_image022

Make sure that the Property and the Label matches with the string which we framed. These things to be in sync. Otherwise, it may not be updated on the config file appropriately.

20. Now, build the application, we will get the MSI as output. While installing the MSI, it will ask for the config Values.

clip_image024

Download the Source Here

Happy Coding,

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation