Environment.SetEnvironmentVariable creates, modifies, or deletes an environment variable stored in the current process or in the Windows operating system registry key reserved for the current user or local machine. The SetEnvironmentVariable(String, String,EnvironmentVariableTarget)
method lets you define an environment variable that is available to all processes that run on a machine (the EnvironmentVariableTarget.Machine value), to all processes run by a user (the EnvironmentVariableTarget.User value), or to the current process (the Process value). Per-machine and per-user environment variables are copied into the environment block of the current process. However, environment variables that are unique to the current process environment block persist only until the process ends.
Environment.GetEnvironmentVariable retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.
EnvironmentVariableTarget specifies the location where an environment variable is stored or retrieved in a set or get operation.
- Machine: The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment key in the Windows operating system registry.
- Process: The environment variable is stored or retrieved from the environment block associated with the current process.
- User: The environment variable is stored or retrieved from the HKEY_CURRENT_USER\Environment key in the Windows operating system registry.
So you can use
Environment.SetEnvironmentVariable("MessageKey", "MessageValue", EnvironmentVariableTarget.Machine);
and
Environment.GetEnvironmentVariable("MessageKey", EnvironmentVariableTarget.Machine);