Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Show the Use of GetEnvironmentVariable() Method of Environment Class
The GetEnvironmentVariable() method of the Environment class in C# is used to retrieve the value of an environment variable. This method is essential for accessing system-wide and user-specific environment variables such as PATH, TEMP, or custom variables set by applications.
Syntax
Following is the basic syntax for the GetEnvironmentVariable() method
public static string GetEnvironmentVariable(string variable)
There is also an overloaded version that accepts an EnvironmentVariableTarget parameter
public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
Parameters
variable A string containing the name of the environment variable.
target (optional) Specifies the location of the environment variable (Process, User, or Machine).
Return Value
Returns the value of the environment variable as a string, or null if the variable does not exist.
Using GetEnvironmentVariable() for System Variables
This example shows how to retrieve common system environment variables
using System;
class Program {
static void Main() {
string path = Environment.GetEnvironmentVariable("PATH");
string temp = Environment.GetEnvironmentVariable("TEMP");
string userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
Console.WriteLine("PATH: " + (path ?? "Not found"));
Console.WriteLine("TEMP: " + (temp ?? "Not found"));
Console.WriteLine("USERPROFILE: " + (userProfile ?? "Not found"));
}
}
The output of the above code is
PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem TEMP: C:\Users\Username\AppData\Local\Temp USERPROFILE: C:\Users\Username
Using GetEnvironmentVariable() with Specific Targets
This example demonstrates how to search for environment variables in specific locations
using System;
class Program {
static void Main() {
string variable = "PATH";
string processValue = Environment.GetEnvironmentVariable(variable, EnvironmentVariableTarget.Process);
string userValue = Environment.GetEnvironmentVariable(variable, EnvironmentVariableTarget.User);
string machineValue = Environment.GetEnvironmentVariable(variable, EnvironmentVariableTarget.Machine);
Console.WriteLine("Process PATH: " + (processValue != null ? "Found" : "Not found"));
Console.WriteLine("User PATH: " + (userValue != null ? "Found" : "Not found"));
Console.WriteLine("Machine PATH: " + (machineValue != null ? "Found" : "Not found"));
// Check for a custom variable
string customVar = Environment.GetEnvironmentVariable("MY_CUSTOM_VAR");
if (customVar != null) {
Console.WriteLine("Custom Variable: " + customVar);
} else {
Console.WriteLine("Custom variable 'MY_CUSTOM_VAR' does not exist");
}
}
}
The output of the above code is
Process PATH: Found User PATH: Not found Machine PATH: Found Custom variable 'MY_CUSTOM_VAR' does not exist
Environment Variable Target Comparison
| Target | Scope | Persistence |
|---|---|---|
| Process | Current application process only | Lost when process ends |
| User | Current user account | Persists across sessions |
| Machine | All users on the system | System-wide and persistent |
Conclusion
The GetEnvironmentVariable() method provides a reliable way to retrieve environment variable values in C#. It supports different target scopes and returns null for non-existent variables, making it essential for configuration management and system integration tasks.
