0

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:> 
4
  • 2
    You cannot. Enviroment variables only propagate to children. Commented Aug 21, 2024 at 14:55
  • 1
    The default value for EnvironmentTarget is Process. Commented Aug 21, 2024 at 15:05
  • This question is similar to: take combo box choice and use choice as SETX. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 21, 2024 at 15:16
  • This will set it for the current and any future child processes. You can create a new cmd.exe using Process.Start(), and it will inherit that env var. Commented Aug 21, 2024 at 15:37

2 Answers 2

1

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.

Thats a very incorrect way to do it, a file on a folder is better.

the Environment.SetEnvironmentVariabl works only for current process or children process.

If you want to set an environment variable in C# that other processes can access, you need to define it at the user or system level. Here's how you can do that: (requires administrative privileges)

Environment.SetEnvironmentVariable("TEST1", "1", EnvironmentVariableTarget.Machine); 
Sign up to request clarification or add additional context in comments.

2 Comments

OP states: "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"
I dont want to set permanent environment variables, I just want to export the environment variables into the environment that the current shell is running. Using bash, a script would call "export VARIABLE=value" and that VARIABLE now exists within the bash environment so that whatever is looking for it sees it. CMD scripts ironically default the opposite, always EXPORTING the variables unless SETLOCAL is called first. I need to be able to EXPORT from a C# program but it appears I need to parse the gitverison json some other way.
0

There is not a way to scope environment variables to the parent process.

One potential option is setting the variables in the C# application as User- or Machine-scoped, then unset after the batch processing has run. If there are multiple processes sharing these variables, then each can be scoped to each process via a prefix - Process1Var1, Process2Var1, etc.

An alternative to the environment variables would be using these values as command-line args for the batch process and call that within the C# application. Another could be saving the values to a file and reading those from the batch process.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.