Creating installation package for Windows Forms Applications

August 5, 2011

Over the years I have created numerous Windows Forms Applications for a variety of purposes. Normally this is for my own use or for use by an IT team but on a recent project I was developing a Windows Forms Application which is to be used by regular Joe Blogs end users. This meant I needed to provide an installer to ensure all the prerequisites were installed i.e. .Net framework, etc. While it is not something I have had to do a lot of I have created a few installers in my time, however most of them have used the OOTB Setup Project template from Visual Studio.

On this project I am still using the OOTB Setup Project, however I have also had to capture some additional parameters during the installation such as the URL of the web service to connect to. I knew this would be possible but I had no idea what would be involved but after some research I was able to find an excellent MSDN article on Using a Custom Action to Display a Message at Installation. While the idea is slightly different the principle is the same and you can easily adjust this. In my case I capture the web service URL and update the settings file with the URL supplied. I found a good article which details the steps required to update the configuration file. My code is slightly different in that it is within the custom installer class so I have included this below as well.

Code in Customer Installer Class

Code Snippet
  1. public override void Install(System.Collections.IDictionary stateSaver)
  2.         {
  3.             base.Install(stateSaver);
  4.             try
  5.             {
  6.                 string webserviceURL = Context.Parameters["PARAMETER1"];
  7.                 
  8.                 Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
  9.                 ApplicationSettingsGroup group = config.GetSectionGroup("applicationSettings") as ApplicationSettingsGroup;
  10.                 ClientSettingsSection settingsSection = group.Sections["Applications.Properties.Settings"] as ClientSettingsSection;
  11.                 SettingElement webServiceSetting = settingsSection.Settings.Get("SETTINGNAME");
  12.                 webServiceSetting.Value.ValueXml.InnerText = webserviceURL;
  13.                 config.Save(ConfigurationSaveMode.Full, true);
  14.             }
  15.             finally
  16.             {
  17.                 base.Dispose();
  18.             }
  19.         }


%d bloggers like this: