Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Reload Systems environment variables

When i'm starting a C# Process, the system environment variables are changed by some other processes. So i want to reload the environment into my process, because this application starts also child processes which need to have the udated environment. This is the way I'm trying to get and set the environment variables:

Dictionary<String,String> uservars= new Dictionary<String,String>();
  Dictionary<String, String> sysvars = new Dictionary<String, String>();
  foreach (System.Collections.DictionaryEntry de in System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine))
    sysvars.Add(de.Key.ToString().ToUpper(),de.Value.ToString());

  foreach (System.Collections.DictionaryEntry de in System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User))
  {
    uservars.Add(de.Key.ToString().ToUpper(), de.Value.ToString());
  }
  Dictionary<string, string> newdict = new Dictionary<string, string>();
  foreach (KeyValuePair<String, String> kvp in uservars)
  {
    if (sysvars.ContainsKey(kvp.Key))
    {
      newdict.Add(kvp.Key, kvp.Value + ";" + sysvars[kvp.Key]);
      sysvars.Remove(kvp.Key);
    }
    else
      newdict.Add(kvp.Key, kvp.Value);
  }
  foreach (KeyValuePair<string, string> kvp in sysvars)
    newdict.Add(kvp.Key, kvp.Value);

  foreach (KeyValuePair<string, string> kvp in newdict)
  {
    System.Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
  }

The problem is, that the environment variables I get with System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User) are the same like the Variables when my application has started. But that are not the update variables.

Is there any way to get these variables directly from the system.

like image 520
user1829716 Avatar asked Oct 05 '22 13:10

user1829716


1 Answers

There are 3 sets of environment variables - the global variables from the machine, the user's saved variables and the variables of the current process

Environment.SetEnvironmentVariable(key, value) Sets the variables of the current process, it does not change the global or user's variables, to read the current process variables you need to use Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)

When you run a child process your process environment variables are copied into the child process environment variable.

Update (and answer to the comment):

I think you misunderstand how environment variables work, they can only be used to pass data from a parent process to a child process during the process creation, they can not be used to pass data between processes.

This is how environment variables work:

Each process has it's own private copy of the environment variables, each process can change that private copy whenever it wants, it's a private copy that has no effect what so ever on the rest of the system.

When one process starts another process the parent's private environment variables are copies to the new child process, after that operation the new child process has a private copy of the environment variable that is no longer connected to the parent process.

There are machine and user environment variables stored in the registry, those are values that are passed the the first process on boot (and, to save reboots, if you send the right notifications windows explorer will load them when starting a new process) changing a process's environment variables does not change the user/machine settings, changing the user/machine setting does not effect any running process.

For example, run two command (cmd.exe) windows, change the environment in one using the SET command, then run the SET command in the other window and see that nothing has changed.

So, there is no such thing as the "system's current environment", if one process changes its environment there is no way for another process to discover and read this change (except to use APIs that let debuggers read other process memory, but you really don't want to go there).

like image 175
Nir Avatar answered Oct 10 '22 04:10

Nir