Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Environment variables in C# that persist after execution is complete

Tags:

c#

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.

like image 926
user1902876 Avatar asked Dec 14 '12 03:12

user1902876


1 Answers

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.

like image 152
Jordão Avatar answered Oct 12 '22 04:10

Jordão