System.Environment Class
Namespace: System
Provides information about, and means to manipulate, the current environment and platform.
Summary
The System.Environment
class in the .NET Framework provides static properties and methods that allow you to access information about the current environment and platform, such as the operating system version, the .NET runtime version, command-line arguments, environment variables, and exit codes.
Remarks
The Environment
class is sealed and cannot be inherited. All members are static, so you can access them directly using the class name (e.g., Environment.NewLine
). This class is fundamental for writing platform-independent applications or for retrieving configuration details at runtime.
Members
Properties
Name | Description |
---|---|
CommandLine |
Gets the fully qualified path of the default (current) assembly's executable file, including the file name, and the command-line arguments that were used to start the current process. |
CurrentDirectory |
Gets or sets the fully qualified path of the current working directory. |
ExitCode |
Gets or sets the exit code of the current process. |
HasShutdownStarted |
Gets a value indicating whether the current process is shutting down. |
MachineName |
Gets the NetBIOS name of the current computer. |
NewLine |
Gets the newline character or characters available on the current platform. |
OSVersion |
Gets an operating system version object that describes the current operating system or the remote computer. |
ProcessorCount |
Gets the number of processors on the current machine. |
ProcessPath |
Gets the fully qualified path of the current process executable. |
StackTrace |
Gets the file name of the source file that contains the call stack entry. |
SystemDirectory |
Gets the name of the drive and four-digit year of the current date and time. |
TickCount |
Gets the number of ticks that have elapsed since the system was started. |
TickCount64 |
Gets the number of ticks that have elapsed since the system was started as a 64-bit integer. |
UserDomainName |
Gets the domain name of the user. |
UserName |
Gets the name of the current user. |
Version |
Gets a string representing the version of the current .NET Framework installation. |
WorkingSet |
Gets the amount of virtual memory available to the process for allocation. |
Methods
Name | Description |
---|---|
Exit(int exitCode) |
Terminates this process and provides an exit code. |
FailFast(string message) |
Forces the process to terminate. |
GetCommandLineArgs() |
Gets the command line arguments of the current process. |
GetEnvironmentVariable(string variable) |
Retrieves the value of the specified environment variable. |
GetEnvironmentVariables() |
Retrieves all environment variables for the current process. |
IsPrivilegedProcess |
Gets a value indicating whether the current process has elevated privileges. |
SetEnvironmentVariable(string variable, string value) |
Sets the value of an environment variable. |
Example Usage
Here's a simple example demonstrating how to use some members of the System.Environment
class:
using System;
public class EnvironmentDemo
{
public static void Main(string[] args)
{
Console.WriteLine($"Operating System: {Environment.OSVersion}");
Console.WriteLine($"Machine Name: {Environment.MachineName}");
Console.WriteLine($"User Name: {Environment.UserName}");
Console.WriteLine($"Current Directory: {Environment.CurrentDirectory}");
Console.WriteLine($"New Line: \"{Environment.NewLine}\"");
Console.WriteLine($"System Directory: {Environment.SystemDirectory}");
Console.WriteLine($"Processor Count: {Environment.ProcessorCount}");
Console.WriteLine($".NET Version: {Environment.Version}");
string pathVar = Environment.GetEnvironmentVariable("PATH");
Console.WriteLine($"\nPATH environment variable (first 50 chars): {pathVar?.Substring(0, Math.Min(pathVar.Length, 50))}...");
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
// Example of setting an environment variable (scoped to the current process)
Environment.SetEnvironmentVariable("MY_CUSTOM_VAR", "HelloWorld");
Console.WriteLine($"MY_CUSTOM_VAR: {Environment.GetEnvironmentVariable("MY_CUSTOM_VAR")}");
// Example of exiting with a specific code
// Environment.Exit(0);
}
}