The Environment class is a static class that allows you to query and control aspects of the execution environment of a .NET application.
With it, you can obtain information about the operating system, the user, or the process, as well as read environment variables and console arguments.
What the Environment class offers
The Environment class provides static methods and properties that give information about the operating system, available memory, regional settings, environment variables, and the state of the application’s process. The class is designed to retrieve system data and manage settings related to the execution environment.
Among the most common functionalities of the Environment class are:
- Obtaining information about the operating system and platform.
- Retrieving system environment variables.
- Querying system and process data.
- Terminating the process with an exit code.
The Environment class does not have a constructor, as it is a static class. All its methods and properties are static, meaning they are accessed directly without needing to create an instance of the class.
Main properties of Environment
The Environment class provides various properties that allow you to obtain critical information about the system and the execution environment. Below are some of the most commonly used properties.
MachineName
This property returns the name of the machine (host) on which the application is running.
using System;
class Program
{
static void Main()
{
// Get the machine name
string machineName = Environment.MachineName;
Console.WriteLine("Machine name: " + machineName);
}
}OSVersion
The OSVersion property returns a Version object containing the operating system version. This is useful when you need to adapt the application’s behavior to different Windows versions or platforms.
using System;
class Program
{
static void Main()
{
// Get the operating system version
string osVersion = Environment.OSVersion.ToString();
Console.WriteLine("Operating system version: " + osVersion);
}
}CurrentDirectory
The CurrentDirectory property provides the path of the current directory where the process is running. It is commonly used when manipulating files in the file system, as it allows you to know the execution location of the application.
using System;
class Program
{
static void Main()
{
// Get the current directory
string currentDirectory = Environment.CurrentDirectory;
Console.WriteLine("Current directory: " + currentDirectory);
}
}UserName
This property returns the name of the account running the application. It is useful for displaying contextual information, although it should not be used as an authentication mechanism.
using System;
class Program
{
static void Main()
{
// Get the current user name
string userName = Environment.UserName;
Console.WriteLine("User name: " + userName);
}
}ProcessorCount
ProcessorCount returns the number of logical processors available for the process. This information can guide the degree of parallelism of an application.
using System;
class Program
{
static void Main()
{
// Get the number of logical processors
int processorCount = Environment.ProcessorCount;
Console.WriteLine("Number of logical processors: " + processorCount);
}
}Environment methods
In addition to properties, the Environment class provides several useful methods that allow you to manage various aspects of the execution environment.
GetEnvironmentVariable
The GetEnvironmentVariable method allows you to access system or user environment variables. Environment variables provide information about the operating system or application configuration and are common on Unix and Windows systems.
using System;
class Program
{
static void Main()
{
// Get a system environment variable
string pathVariable = Environment.GetEnvironmentVariable("PATH");
Console.WriteLine("PATH environment variable: " + pathVariable);
}
}SetEnvironmentVariable
The SetEnvironmentVariable method allows you to modify an environment variable. Without specifying an EnvironmentVariableTarget, the change only affects the current process.
using System;
class Program
{
static void Main()
{
// Set an environment variable for the current process
Environment.SetEnvironmentVariable("MY_CUSTOM_VARIABLE", "12345");
string customVar = Environment.GetEnvironmentVariable("MY_CUSTOM_VARIABLE");
Console.WriteLine("Custom environment variable: " + customVar);
}
}Exit
The Exit method allows you to terminate the application’s process in a controlled manner. This method can take an exit code that indicates the state with which the application terminates, where 0 generally means success and any other value indicates an error.
using System;
class Program
{
static void Main()
{
// Terminate the process with exit code 0 (success)
Environment.Exit(0);
}
}GetCommandLineArgs
This method returns an array of strings containing the command-line arguments with which the process was started. It is useful in console applications for handling input parameters provided by the user when running the application.
using System;
class Program
{
static void Main()
{
// Get the command-line arguments
string[] args = Environment.GetCommandLineArgs();
foreach (var arg in args)
{
Console.WriteLine(arg);
}
}
}Use cases for Environment
Adapting behavior to the platform
With the OSVersion property, you can customize the application’s behavior according to the operating system. For example, if running on Windows, the application might have a different directory configuration or graphical interface interaction.
using System;
class Program
{
static void Main()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
Console.WriteLine("The application is running on Windows.");
}
else
{
Console.WriteLine("The application is not running on Windows.");
}
}
}Adjusting available parallelism
The ProcessorCount method is useful when you want to adapt the number of worker threads or parallel processes of an application according to the hardware capacity of the system.
using System;
class Program
{
static void Main()
{
int numThreads = Environment.ProcessorCount;
Console.WriteLine($"Starting the application with {numThreads} worker threads.");
}
}Managing environment variables
Through the GetEnvironmentVariable and SetEnvironmentVariable methods, it is possible to manage system and application configurations without having to manually modify configuration files.