public static void SetEnvironmentVariables()

in SmvLibrary/Utility.cs [799:836]


        public static void SetEnvironmentVariables(ProcessStartInfo processInfo, SMVEnvVar[] env, TextWriter logger)
        {
            if (processInfo == null || env == null)
                return;

            Dictionary<string, string> variablesSet = new Dictionary<string, string>();
            variablesSet.Add("PATH", Environment.GetEnvironmentVariable("PATH"));
            String varKey = null, varValue = null;

            Log.LogInfo("Setting environment variables..", logger);


            foreach (SMVEnvVar envVar in env)
            {
                varKey = envVar.key;
                varValue = envVar.value ?? String.Empty;

                // We need to do this because Environment.ExpandEnvironmentVariable does not expand environment variables specific to a process

                foreach (KeyValuePair<string, string> pair in variablesSet)
                {
                    varValue = Regex.Replace(varValue, String.Format(CultureInfo.InvariantCulture, "%{0}%", pair.Key), pair.Value, RegexOptions.IgnoreCase);
                }

                varValue = ExpandSmvVariables(Environment.ExpandEnvironmentVariables(varValue));

                if (variablesSet.ContainsKey(varKey))
                    variablesSet.Remove(varKey);

                variablesSet.Add(varKey, varValue);

                if (processInfo.EnvironmentVariables.ContainsKey(varKey))
                    processInfo.EnvironmentVariables.Remove(varKey);

                Log.LogInfo(String.Format(CultureInfo.InvariantCulture, "Setting environment variable: {0}={1}", varKey, varValue), logger);
                processInfo.EnvironmentVariables.Add(varKey, varValue);
            }
        }