C# Environment
last modified October 15, 2026
In this article we show how to work with the environment and platform in C#.
The Environment class provides information about the current
environment and platform. It is located in the System
namespace.
C# system information
In the following example we print some system information.
var osv = Environment.OSVersion;
Console.WriteLine($"OS: {osv.Platform}");
Console.WriteLine($"Version: {osv.Version}");
Console.WriteLine($"OS & version: {osv.VersionString}");
string userName = Environment.UserName;
string userDomName = Environment.UserDomainName;
Console.WriteLine($"User name: {userName}");
Console.WriteLine($"User domain name: {userDomName}");
The example prints the OS name and version, user name, and user domain name.
var osv = Environment.OSVersion;
The Environment.OSVersion property gets the current platform
identifier and version number. On Linux, the version is the version of the
kernel.
$ dotnet run OS: Unix Version: 6.8.0.138 OS & version: Unix 6.8.0.138 User name: jano User domain name: universum
C# common directories
In the next example, we print some common system directories.
string curDir = Environment.CurrentDirectory;
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string myMusicDir = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
string addDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string userProfDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string myPictDir = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Console.WriteLine($"Current directory: {curDir}");
Console.WriteLine($"Desktop directory: {deskDir}");
Console.WriteLine($"My Music directory: {myMusicDir}");
Console.WriteLine($"Application data directory: {addDataDir}");
Console.WriteLine($"User profile directory: {userProfDir}");
Console.WriteLine($"My Pictures directory: {myPictDir}");
The example shows some common directories, including Desktop, My Music,
Application Data, User Profile, and My Pictures. The
Environment.SpecialFolder enumeration has names derived from
Windows; on Linux, they are mapped to the corresponding XDG directories.
$ dotnet run Current directory: /home/jano/Documents/prog/csharp/environment/EnvironmentEx Desktop directory: /home/jano/Desktop My Music directory: /home/jano/Music Application data directory: /home/jano/.config User profile directory: /home/jano My Pictures directory: /home/jano/Pictures
C# command line arguments
The next example prints command line arguments.
string cline = Environment.CommandLine;
Console.WriteLine(cline);
string[] cargs = Environment.GetCommandLineArgs();
foreach (var arg in cargs)
{
Console.WriteLine(arg);
}
The Environment.CommandLine returns the command line as a single
string. To get an array of arguments, we use the
Environment.GetCommandLineArgs method. The first element of the
array is the name of the program.
$ dotnet run "John Doe" gardener 34 /home/jano/Documents/prog/csharp/environment/EnvironmentEx/bin/Debug/net10.0/EnvironmentEx.dll "John Doe" gardener 34 /home/jano/Documents/prog/csharp/environment/EnvironmentEx/bin/Debug/net10.0/EnvironmentEx.dll John Doe gardener 34
C# environment variables
We can also retrieve environment variables.
using System.Collections;
string? user = Environment.GetEnvironmentVariable("USER");
Console.WriteLine(user);
string? shell = Environment.GetEnvironmentVariable("SHELL");
Console.WriteLine(shell);
string? home = Environment.GetEnvironmentVariable("HOME");
Console.WriteLine(home);
var envs = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry de in envs)
{
Console.WriteLine($"{de.Key}: {de.Value}");
}
We can get a specific environment variable with
GetEnvironmentVariable; the method returns null if the
variable does not exist. To retrieve all variables, we use
GetEnvironmentVariables. It returns an IDictionary,
whose entries are DictionaryEntry objects.
$ dotnet run jano /bin/bash /home/jano CLUTTER_IM_MODULE: ibus SDKMAN_VERSION: 5.16.0 PANEL_GDK_CORE_DEVICE_EVENTS: 0 MICRONAUT_HOME: /home/jano/.sdkman/candidates/micronaut/current LC_PAPER: sk_SK.UTF-8 ...
C# process and runtime information
The Environment class also provides information about the current
process and the runtime.
Console.WriteLine($".NET version: {Environment.Version}");
Console.WriteLine($"64-bit OS: {Environment.Is64BitOperatingSystem}");
Console.WriteLine($"64-bit process: {Environment.Is64BitProcess}");
Console.WriteLine($"Processor count: {Environment.ProcessorCount}");
Console.WriteLine($"Process ID: {Environment.ProcessId}");
Console.WriteLine($"Process path: {Environment.ProcessPath}");
Console.WriteLine($"Uptime: {TimeSpan.FromMilliseconds(Environment.TickCount64):d\\.hh\\:mm\\:ss}");
Console.WriteLine($"Working set: {Environment.WorkingSet / 1024 / 1024} MB");
Console.WriteLine($"New line: {Environment.NewLine == "\n"}");
The program prints the version of the .NET runtime, the bitness of the operating system and the process, the number of processors, the ID and the path of the current process, the time elapsed since the system started, and the amount of physical memory mapped to the process.
Console.WriteLine($"New line: {Environment.NewLine == "\n"}");
The Environment.NewLine property contains the newline string for
the current platform. It is \n on Unix systems and
\r\n on Windows.
$ dotnet run .NET version: 10.0.7 64-bit OS: True 64-bit process: True Processor count: 8 Process ID: 79325 Process path: /home/jano/Documents/prog/csharp/environment/EnvironmentEx/bin/Debug/net10.0/EnvironmentEx Uptime: 0.01:41:09 Working set: 31 MB New line: True
Source
In this article we worked with the Environment class in C#.
Author
List all C# tutorials.