BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Reading Environment Variable Values

All Microsoft Windows operating systems include configuration options that are held in environment variables. These contain settings such as default folder paths, user information and computer and operating system details. They can be accessed using C#.

Environment Variables

Environment variables are simple key / value pairs used by the operating system. Each environment variable has a name, held in its key, and an associated value. They hold information that can be used by running processes to ensure correct operation according to user preferences and the system set-up. For example, there are environment variables to hold the name of the current user, the name of the computer and the default locations for the main Windows directory and default application and data folders.

Environment Class

The Environment class is a standard class within the System namespace. It provides information relating to the current environment and operating system and, in some cases, allows the information to be modified. The class contains methods that permit environment variable values to be obtained.

Retrieving a Collection of All Environment Variables

Sometimes it is useful to simply retrieve a list of all of the environment variable names and their values. This can be achieved using the static GetEnvironmentVariables method. The method requires no parameters and returns a series of key / value pairs in a collection that supports the IDictionary interface.

The following sample code shows the use of the GetEnvironmentVariables method. It retrieves all of the available environment variables and outputs them to the console. As the IDictionary interface is used, add using System.Collections; to the top of the code file to reference the correct namespace.

IDictionary envVars = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry envVar in envVars)
{
    Console.WriteLine("{0} : {1}", envVar.Key, envVar.Value);
}

NB: The output of this foreach loop is long and varies according to the operating system's configuration. For these reasons, it has been omitted from this article.

Retrieving a Specific Environment Variable Value

If you wish to determine the value of a specific environment variable, you can use the GetEnvironmentVariable method. This method requires a single parameter containing the name of the environment variable to query. If the environment variable name is valid, the corresponding value is returned in a string. If the environment variable specified is unknown, the method returns null. The parameter value is not case-sensitive.

The specific environment variable names for your system can be determined using the previous code sample. To demonstrate the use of GetEnvironmentVariable, we will retrieve the path of the root folder for the operating system. If this was chosen as the default value when installing Microsoft Windows, the value will be "C:\Windows" or "C:\WINNT". Of course, if the configuration is not the default then the output will be different.

Console.WriteLine(Environment.GetEnvironmentVariable("SystemRoot"));

Expanding Environment Variables

Often the value of an environment variable will be used for display purposes, or for other forms of output, within human-readable text. In these cases, a third member of the Environment class is helpful. The ExpandEnvironmentVariables method allows a string containing special environment variable placeholders to be processed. The returned string has the placeholders replaced with the appropriate values.

A placeholder is defined by surrounding the name of an environment variable with percentage signs (%). An example of this is "%SystemRoot%". Any number of such placeholders may be embedded within the string to be expanded. The following sample demonstrates the method with two replacements.

string expand = "Your root folder is %SystemRoot% on computer %COMPUTERNAME%.";
Console.WriteLine(Environment.ExpandEnvironmentVariables(expand));
27 April 2008