Home >Backend Development >C++ >How Do I Manage Environment Variables in C#?
Working with Environment Variables in C#
Accessing and modifying environment variables is a frequent programming need. The System.Environment
class in C# provides the tools to handle this efficiently.
Retrieving Environment Variables
Use the GetEnvironmentVariable
method to obtain the value of an environment variable:
<code class="language-csharp">string value = System.Environment.GetEnvironmentVariable("variableName", EnvironmentVariableTarget.Process);</code>
The EnvironmentVariableTarget
parameter (optional, defaults to Process
) defines the variable's scope: Machine
, Process
, or User
.
Modifying Environment Variables
To change the value of an environment variable, employ the SetEnvironmentVariable
method:
<code class="language-csharp">System.Environment.SetEnvironmentVariable("variableName", "newValue", EnvironmentVariableTarget.Process);</code>
As before, EnvironmentVariableTarget
determines the variable's scope. Remember that changes made with Machine
or User
scope might require administrator privileges.
The above is the detailed content of How Do I Manage Environment Variables in C#?. For more information, please follow other related articles on the PHP Chinese website!