0

I start child process from parent process using Process.Start(). In child process I create new variable calling

Environment.SetEnvironmentVariable("MessageKey", "MessageValue"); After exit of child process I want to read aforementioned variable from parent process using

var env = childProcess.StartInfo.EnvironmentVariables;
string MessageValue = env["MessageKey"];

variable is absent. How to send message from child to parent process using environment variable in C#?

Vlad
  • 1,977
  • 19
  • 44
  • 1
    That ain't going to work. Use proper IPC methods to do this. http://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro – OldProgrammer Jan 03 '17 at 02:44

1 Answers1

2

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.

  1. 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.
  2. Process: The environment variable is stored or retrieved from the environment block associated with the current process.
  3. 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);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • 1
    without restarting parent? probably Reload is needed? – Vlad Jan 03 '17 at 03:01
  • 1
    It should be noted that there is no parent/child relationship here. If _any_ process sets a `Machine` value then any other process can pick it up. Creating environment variable names based on process id's would help somewhat while resulting in a delightful crufting of the registry. – HABO Jan 03 '17 at 04:41
  • @Mohit Shrivastava Thank you, I used your approach (I also saw it in MSDN, it has some drawbacks - other processes can interfere, so I was searching something else). And I fount following solution that **directly** solves my question but it's verbose and Win API functions used in it are not supported in Windows 10: https://blog.gapotchenko.com/eazfuscator.net/reading-environment-variables – Vlad Jan 05 '17 at 02:01
  • Also I should add to your answer that **parent** process block of env variables are copied to child process. And it's easy to modify them and pass message from parent to child. – Vlad Jan 05 '17 at 02:12