C# set environment variable
Asked Answered
S

2

21

I have problem with setting environment variables using C#.

I need to modify some environment variables on some circumstances. For example I need to modify NDSRC variable.

I use:

Environment.SetEnvironmentVariable("MY_VARIABLE", "value", EnvironmentVariableTarget.Machine);

This works fine.

Next i run some script whitch uses the variable. And now there is a problem, because script does not see the variable.

Example: Set Path variable (add a directory at the end) using

string path = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) + ";c:\\";
Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.Machine);

Open windows command line (Start->run->cmd.exe).

In command line type cmd

The system can not find cmd.exe: 'cmd' is not recognized as an internal or external command, operable program or batch file.

If you check Windows settings - > Environment Variables, Path is correctly set to new value. If you check in opened command prompt, it is also set.

Shane answered 19/3, 2012 at 16:53 Comment(0)
S
7

Unfortunately, you need to restart your process before environment variables can be refreshed. See this MSDN post.

Surfactant answered 19/3, 2012 at 16:55 Comment(1)
Hello. That is not the problem. I use Environment.SetEnvironmentVariable("MY_VARIABLE", "value", EnvironmentVariableTarget.Process); to set enviroment variable for my process, and it works. The problem is that outside of my process, even if I see changed variable, any other process seem not to see it. In the example I just add something to Path variable. After that, the system does not see path variable at all.Shane
M
2

It is by design that the variables are inherited when the process starts, and remain fixed after that.

However there is no reason why you can’t just go in to read the relevant registry keys periodically and update your process’ environment variables manually from that. In fact, this is the right thing to do if you’re after up-to-date values.

Basically, the registry stores a template for environment variables, and that’s what you edit via "Windows settings - > Environment Variables". When you do that, Windows broadcasts a message to all interested parties. Any such parties can then re-create their copy of the environment variables from the registry.

I am not aware of any ready-made function that you can just call to perform this re-creation, however, so you’ll probably have to write your own.

Mychal answered 19/3, 2012 at 17:23 Comment(5)
There is problem with setting env variables with Environment.SetEnvironmentVariableShane
I found a workaround: From code create Process (cmd.exe). In this process execute "setx /M Variable Value" command. This will modify global env variable and will not cause the problem.Shane
@user641426: Global anything tends to cause a problem over time.Phenosafranine
For me, the "setx /M" failed because of a 256 character limit (my path var was too long).Tropology
If you kill and restart all explorer.exe processes, then anything spawned by explorer/start-menu/start-run (most things) will get the new PATH.Tropology

© 2022 - 2024 — McMap. All rights reserved.