I\'m trying to set a system environment variable in my application, but get an SecurityException. I tested everything I found in google - without success.
Here
In addition to David Heffernan's answer, here is a function that avoids a few gotchas for setting and appending a value to an environment variable:
void AddToEnvironmentVariable(string variable, string newValue, EnvironmentVariableTarget target)
{
var content = Environment.GetEnvironmentVariable(variable, target) ?? string.Empty;
if (content.Contains(newValue))
return;
var varBuilder = new StringBuilder(content);
if (content != string.Empty && !content.EndsWith(';'))
varBuilder.Append(";");
varBuilder.Append(newValue);
var finalValue = varBuilder.ToString();
Environment.SetEnvironmentVariable(variable, finalValue, EnvironmentVariableTarget.Process);
if (target != EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable(variable, finalValue, target);
}
It handles proper appending using ; and also ensures that subsequent calls to GetEnvironmentVariable contain the added value by setting in with the Processtarget.