Launch Condition - Does Environment Variable Exist - Advanced Installer Community
jsfetzik
Posts: 4
Joined: Fri Nov 02, 2012 5:28 pm

Launch Condition - Does Environment Variable Exist

My MSI is dependent upon the existence of an environment variable. If this variable does not exist or is blank I want to stop the MSI.

I am assuming I would use the launch conditions feature to do this. Checking for blank seems straight forward, but how can I check for existence of the environment variable?
Dan
Posts: 4530
Joined: Wed Apr 24, 2013 3:51 pm

Re: Launch Condition - Does Environment Variable Exist

Hello,

We don't have predefined support to search if an environment variable exists or not.
In this case, you can create a sample custom action that performs this search. For example, you can use the Environment.GetEnvironmentVariable Method (String) to retrieve the value of an environment variable.
Also, please take a look on the How to integrate and debug custom actions? article which may be useful to you.

A sample code that performs this check is the following:

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.Windows.Forms;

namespace Check_EnvironmentVariable_existance
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CheckEnviromentVariable(Session session)
        {
            session.Log("Begin CheckEnviromentVariable");

            // Check whether the environment variable exists.
            string envirVar = Environment.GetEnvironmentVariable("EnvirNameTest");

            // the environment variable does not exist
            if (envirVar == null)
            {
                session.Log("The environment variable does not exist");
                MessageBox.Show("The EnvirName does no exist. The installation will stop!");
                return ActionResult.Failure;
            }
            else
            {
                return ActionResult.Success;
            }
        }
    }
}
You can also use a script powershell in order to check if the environment variable exists. For the powershell code approach, you can use the Run Windows PowerShell Script custom action.

The sample project is attached to this thread, so if you are interested to take a look directly at my project, you are more than welcome to download the ZIP file.

Should there be any difficulty you encounter implementing something, please do not hesitate to contact me and I will gladly assist.

Best regards,
Dan
Attachments
EnvironmentVariable existence check.zip
(122.53 KiB) Downloaded 274 times
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”