Friday, 2 June 2017

Writing ProductVersion to a text file during installation

Today I was tasked with creating a text file during the running of an MSI installer, and writing the app's ProductVersion to it, so that our remote admin tools could read at a glance what versions our users were running.

First of all I needed to create an Installer Class in the app, and then override the Install method like so, capturing an input parameter and writing it to a text file:

 public override void Install(IDictionary savedState)
        {
            base.Install(savedState);

            string version = Context.Parameters["Version"];

            string path2 = "C:\\MyFile.txt";

            try
            {
                FileStream fs1 = new FileStream(path2, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter writer1 = new StreamWriter(fs1);
                writer1.Write("Version:" + "\r\n" +
                             version);
                writer1.Close();
            }
            catch (Exception)
            {
               //do something here if you need to
            }
}

Then, in the Setup project,  I added Custom Action. Right-click on the project, go to View > Custom Actions. Right-click on the install folder, and select Add Custom Action. Browse to the Application folder, select the primary output, and click 'Add Output'. Then, select the thing you've just added, and in the properties window, do the following:


No comments:

Post a comment

Comments are moderated, so you'll have to wait a little bit before they appear!