I need to setup an environment variable from a C# program so batch files that run later can use this newly created variable. I have tried using:
Environment.SetEnvironmentVariable("USRNAM", "My Name",
EnvironmentVariableTarget.Process);
After this statement I have a breakpoint setup and when it gets to this breakpoint, I go to a Command Prompt, issue the following command:
C:\Users\Lenovo>SET USRNAM
I get:
Environment variable USRNAM not defined.
How can I set an Environment variable that persists after the C# program execution has completed?
Suggestions are very much appreciated.
Try to use either EnvironmentVariableTarget.User
or EnvironmentVariableTarget.Machine
, depending on whether you'd like the variable to be created for the current user or for all users.
Look here for information on that enumeration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With