C # – Working with Environment Variables

Mauricio SilvaBlogLeave a Comment

Using environment variables is a common practice during development tasks. This makes it easy to change configuration values depending on scope without changes in your source code. Managing these variables are important to avoid having unexpected values during run time.

Introduction

In this post, I will introduce concepts related to environment variables and show how we can treat them in a program using the C # language.

The Environment class can be used to get information about the current environment of the platform on which we are working.

It can be used to retrieve information such as command line arguments, exit code, environment variable settings, call stack content, time since the last system boot, and the Common Language Runtime (CLR) version.

Remember that this same information could be stored in a configuration file or in any other data source, however, the environment variables are quite easy to use.

Environment Variables in C#

The GetEnvironmentVariable (String) method retrieves an environment variable from the current process environment block, which includes the following environment variables:

  • All environment variables per machine (Machine) that are defined at the time the process is created, along with their values;
  • All environment variables per user (User) that are defined at the time of the process is created, along with their values;
  • Any variable added to the process block while the process is running by calling SetEnvironmentVariable (String, String) or SetEnvironmentVariable (String, String, EnvironmentVariableTarget) with a target value of EnvironmentVariableTarget.Process.

To retrieve all environment variables together with their values, call the GetEnvironmentVariables method (environment variable names are not case-sensitive).

The EnvironmentVariableTarget enumeration can be used by certain overloads of the SetEnvironmentVariable, GetEnvironmentVariable, and GetEnvironmentVariables methods to specify the location or destination, where the name and value of an environment variable are stored or retrieved.

The destination can be one of three locations:

  • The environment block associated with the current process;
  • The Windows operating system registry key reserved for environment variables associated with the current user;
  • The registry key reserved for environment variables associated with all users on the local machine.

Caution and restrictions on the use of environment variables

Using environment variables (in any programming language) is a common practice during development tasks, but you should be careful to use it in production environment, because using environment variables can cause unexpected values.

The use of environment variables is not not recommended for all situations. Pay attention because the concept of a environment variable is the same of a global variable, and this can cause problems during the execution of programs.

Environment variables are good way to set parameters that are used by processes that you do not have direct control over, but it should not be used for configuration values. In this case, instead of environment variables, you can use configuration files to store values to be used during a program execution.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.