C# set environment variable

62,218

Solution 1

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

Solution 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.

Share:
62,218
Pablo notPicasso
Author by

Pablo notPicasso

Updated on March 21, 2020

Comments

  • Pablo notPicasso
    Pablo notPicasso about 4 years

    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.

  • Pablo notPicasso
    Pablo notPicasso about 12 years
    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.
  • Pablo notPicasso
    Pablo notPicasso about 12 years
    There is problem with setting env variables with Environment.SetEnvironmentVariable
  • Pablo notPicasso
    Pablo notPicasso about 12 years
    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.
  • Eric J.
    Eric J. over 11 years
    @user641426: Global anything tends to cause a problem over time.
  • Curtis Yallop
    Curtis Yallop about 10 years
    For me, the "setx /M" failed because of a 256 character limit (my path var was too long).
  • Curtis Yallop
    Curtis Yallop about 10 years
    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.