Given a stupidly simple program:
Console.WriteLine("Hello World."); Console.WriteLine("Set TEST1=1"); Environment.SetEnvironmentVariable("TEST1", "1"); Console.WriteLine("Inside: TEST1={0}", Environment.GetEnvironmentVariable("TEST1")); then build it, and run from a command prompt.
C:>set | find "TEST" C:>cs_helloworld.exe Hello World. Set TEST1=1 Inside: TEST1=1 C:>set | find "TEST" C:>set TEST1 Environment variable TEST1 not defined How does one set an environment variable that survives the process, ie set environment variable for the parent process, ie the cmd shell? In a bash script, you'd use 'export TEST1=1' and afterwards the shell environment would have TEST1=1 in its environment variables.
Environment.SetEnvironmentVariable("TEST1","1",EnvironmentTarget.User) is setting REGISTRY keys, which is not appropriate... I dont want these to survive restarts of the cmd shell, just the current cmd shell's environment.
My use case scenario is parsing a JSON file generated by GitVersion and dumping it into the environment variables for a batch script on a build server.
{ "VER_Major": 3, "VER_Minor": 0, "VER_Patch": 3 } C:>cs_parsejson.exe Running 'gitversion' C:>set | find "VER_" VER_Major=3 VER_Minor=0 VER_Patch=3 C:>
EnvironmentTargetisProcess.Process.Start(), and it will inherit that env var.