C# Reference ยท Files and I/O
The Environment class
Reference for System.Environment in C#: read and set environment variables, get command-line arguments, set exit codes and use Exit and FailFast.
System.Environment is a static class that describes the process your program runs in. It gives you environment variables, command-line arguments, the exit code, the current directory, the newline sequence and a few facts about the machine such as the processor count and the operating system.
Most of its members are cheap properties and static methods. The ones you reach for most often are GetEnvironmentVariable, GetCommandLineArgs, ExitCode, NewLine and Exit.
string? home = Environment.GetEnvironmentVariable("HOME"); Environment.ExitCode = 2;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static class Environment { public static string? GetEnvironmentVariable(string variable); public static void SetEnvironmentVariable(string variable, string? value); public static IDictionary GetEnvironmentVariables(); public static string ExpandEnvironmentVariables(string name); public static string[] GetCommandLineArgs(); public static int ExitCode { get; set; } public static void Exit(int exitCode); public static void FailFast(string? message); public static string NewLine { get; } public static string CurrentDirectory { get; set; } public static int ProcessorCount { get; } public static int ProcessId { get; } public static string? ProcessPath { get; } public static string MachineName { get; } public static bool Is64BitProcess { get; } public static string GetFolderPath(Environment.SpecialFolder folder); }
| Member | Description |
|---|---|
GetEnvironmentVariable(name) |
Returns the value, or null when the variable is not set. Throws ArgumentNullException when name is null. |
SetEnvironmentVariable(name, value) |
Sets the variable for the current process. A null or empty value deletes it. |
GetCommandLineArgs() |
The program path first, then each argument. |
ExitCode |
The code returned to the operating system when the process ends normally. |
Exit(code) |
Ends the process at once with the given code. |
FailFast(message) |
Terminates the process immediately without running finally blocks or finalizers. |
CurrentDirectory |
The working directory used to resolve relative paths. |
Returns: most members return plain values; the two methods that look values up return null when nothing is found.
Errors: SetEnvironmentVariable throws ArgumentException for an empty name or a name that contains =. Exit and FailFast never return.
Usage notes
- Process scope by default. On Windows an overload takes
EnvironmentVariableTarget.UserorMachineto write persistent values. On Linux and macOS only the process scope has an effect. - Case rules follow the operating system. Names are case-insensitive on Windows and case-sensitive elsewhere. Never rely on one spelling working everywhere.
- Prefer
Environment.NewLineonly for text meant for the current platform. For file formats and network protocols write"\n"or"\r\n"explicitly. ExitCodeversusExit: settingExitCodelets the program finish normally, runningfinallyblocks and disposing resources.Environment.Exitstops sooner and skips most cleanup, so use it sparingly. Returning anintfromMainis the cleanest option.FailFastwrites the message to the error stream and creates a crash dump on some platforms. Use it only when continuing could corrupt data.- Configuration. For application settings prefer
IConfiguration(environment variables are one source). Read secrets from the environment rather than hard-coding them, and never print them. GetCommandLineArgs()includes the program path at index 0, unlike theargsparameter ofMain.ProcessPathis the path of the host executable that runs the program (for exampledotnet), which is what you need to start a copy of yourself.
Related
Process,Path,Task- Lessons: Environment, arguments and Process