Home »
.Net »
C# Programs
C# - Environment.SetEnvironmentVariable() Method with Example
In this tutorial, we will learn about the C# Environment.SetEnvironmentVariable() method with its definition, usage, syntax, and example.
By Nidhi Last updated : March 30, 2023
Environment.SetEnvironmentVariable() Method
The Environment.SetEnvironmentVariable() method is used the set value of the environment variable.
Syntax
void Environment.SetEnvironmentVariable(string var, string val);
Parameter(s)
- var: Environment variable name
- val: Value that we store into a variable
Return Value
It does not return any value.
Exception(s)
- System.Security.SecurityException
- System.ArgumentNullException
- System.ArgumentException
C# Example of Environment.SetEnvironmentVariable() method
The source code to demonstrate the use of SetEnvironmentVariable() method of Environment class is given below. The given program is compiled and executed successfully.
using System;
using System.IO;
class Sample {
//Entry point of Program
static public void Main() {
string Var_Name = "VAR1";
string Var_Value = "True";
if (Environment.GetEnvironmentVariable(Var_Name) == null) {
Environment.SetEnvironmentVariable(Var_Name, Var_Value);
Console.WriteLine("Value stored in environment variable");
} else {
Console.WriteLine("Value already stored in environment variable");
}
}
}
Output
Value stored in environment variable
Press any key to continue . . .
C# Environment Class Programs »