Reputation: 55
I am using an environment variable in my program and I am setting the variable value outside Visual Studio. When I try the below line of code, it returns the variable value only if the value was set before opening the current instance of VS. Any changes done later will not have any effect.
var variableValue = Environment.GetEnvironmentVariable("Env_Var");
To receive the new value set, the only way out is to restart VS. It looks like VS caches these values on startup and thus doesn't consider any new changes. However, my question is whether there is any way to refresh the current VS session such that I can read the changed environment variable value & therefore need not go on to restart it again?
Upvotes: 3
Views: 1540
Reputation: 1490
It is possible with a per-machine environment variable. Changes to those are reflected in a running process.
Environment.GetEnvironmentVariable("Env_Var", EnvironmentVariableTarget.Machine)
If it is feasible to use a machine-wide variable depends on your scenario, however.
Upvotes: 2