In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since the last system boot in milliseconds information. By just using some predefined methods we can get the information of the Operating System using the Environment class and GetEnvironmentVariable() method is one of them. This method is used to find the environment variables. It is overloaded in two different ways:
1. GetEnvironmentVariable(String): This method is used to find the environment variables of the current process. It will find all the environment variables with their values. Always remember the name of the environment variable is case sensitive in macOS and Linux but in Windows, they do not case sensitive.
Syntax:
public static string? GetEnvironmentVariable (string varstring);
Where varstring parameter is of string type and it will represent the name of the environment variable.
Return: This method will return the name of the environment variable. Or return null when the environment variable is not found.
Exceptions: This method will throw the following exception:
- SecurityException: This exception occurs only when the caller does not have the given permission to perform this operation.
- ArgumentNullException: This exception occurs when the variable is null.
2. GetEnvironmentVariable(String, EnvironmentVariableTarget): This method is used to find the environment variables of the current process, or from the Windows OS registry key for the local machine or current user. Always remember the name of the environment variable is case sensitive in macOS and Linux but in Windows, they do not case sensitive.
Syntax:
public static string? GetEnvironmentVariable (string varstr, EnvironmentVariableTarget t);
This method will two parameters named varstr and t. Here, varstr represents the name of the environment variable and t represents the EnvironmentVariableTarget value.
Return: This method will return the name of the environment variable. Or return null when the environment variable is not found.
Exceptions: This method will throw the following exception:
- SecurityException: This exception occurs only when the caller does not have the given permission to perform this operation.
- ArgumentNullException: This exception occurs when the variable is null.
- ArgumentException: This exception occurs when the target is not valid EnvironmentVariableTarget value.
Example:
C#
using System;
using System.Collections;
class GFG{
static public void Main()
{
IDictionary data = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry i in data)
{
Console.WriteLine( "{0}:{1}" , i.Key, i.Value);
}
}
}
|
Output:
USER:priya
GOPATH:/Users/priya/Documents/go
rvm_stored_umask:022
rvm_version:1.29.12 (latest)
HOME:/Users/priya
rvm_bin_path:/Users/priya/.rvm/bin
Press any key to continue...