1

I am developing a .NET 7 CLI that use user inputs, files, directories, and environment variables. I want to make sure that the cmd instance's environment variables remain up-to-date even after the program has exited.

Here's what I'm trying to do:

C:\>set :: Show all environment variables
MyEnvironmentVariable=value1

C:\>MyProgram.exe
* Doing this and that *
* Refreshing environment variables *

C:\>set
MyEnvironmentVariable=value2

I've attempted a few solutions, but they didn't work. These include:

  • Requesting ChatGPT to create a separate class that refreshes the environment variables
  • Copying RefreshEnv.cmd from chocolatey and doing
    • System.Diagnostics.Process.Start("C:\\path\\to\\RefreshEnv.cmd")
    • System.Diagnostics.Process.Start("C:\\path\\to\\RefreshEnv.cmd").WaitForExit()
    • new ProcessStartInfo("cmd.exe", "C:\\path\\to\\RefreshEnv.cmd");
JupperTV
  • 11
  • 1
  • 3
    Any changes you make to environment variables *within* a process can't affect the environment variables of the *parent* process. Environment variables just don't work like that. – Jon Skeet Jun 17 '23 at 16:09
  • Does this answer your question? [take combo box choice and use choice as SETX](https://stackoverflow.com/questions/75225452/take-combo-box-choice-and-use-choice-as-setx) – Tu deschizi eu inchid Jun 18 '23 at 02:05

0 Answers0