Skip to main content

How to Read an Environment Variable in .NET C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change between computers, such as a port, a database address or an API key, belong in environment variables. In .NET C#, Environment.GetEnvironmentVariable reads one by name and returns null when it isn't set.

Syntax​

Syntax
string? <value> = Environment.GetEnvironmentVariable("<NAME>");
PartWhat it does
Environment.GetEnvironmentVariableReads a variable from the environment of the running program.
"<NAME>"The variable's name. On macOS and Linux, capitals must match.
<value>The value as a string, or null when the variable isn't set.

The .NET API documentation: "Retrieves the value of an environment variable from the current process." It returns "The value of the environment variable specified by variable, or null if the environment variable is not found."

The Example​

The example prints the value of a variable named GREETING, and whether it's null, first with the variable set and then without it.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which reads Environment.GetEnvironmentVariable("GREETING") into greeting, prints GREETING is: with its value, and prints whether greeting is null.

main.cs
var greeting = Environment.GetEnvironmentVariable("GREETING");

Console.WriteLine($"GREETING is: {greeting}");
Console.WriteLine($"Is it null? {greeting is null}");

Step 2: Set the Variable and Run It​

Set GREETING to Hello and run the program. Then remove the variable and run it again:

Terminal
export GREETING=Hello
dotnet run main.cs
unset GREETING
dotnet run main.cs

A zsh terminal where export GREETING=Hello and dotnet run main.cs print GREETING is: Hello and Is it null? False, and after unset GREETING the same command prints an empty GREETING is: and Is it null? True.

Output
GREETING is: Hello
Is it null? False
GREETING is:
Is it null? True

With the variable set, the method returned "Hello". Without it, it returned null, which string interpolation prints as nothing, so the second GREETING is: line is empty. Check for null before using a value the program needs.

Common Mistakes​

MistakeWhat happensDo this instead
Using the value without checking for nullCode that calls a method on it fails with a NullReferenceExceptionCheck is null, or supply a default
Changing the case of the name on macOS or Linuxnull: GetEnvironmentVariable("home") didn't find HOMEUse the exact name
Setting the variable in one terminal and running the program in anothernull: each terminal has its own variablesSet it in the terminal that runs the program

Troubleshooting​

SymptomLikely causeFix
The value is always nullThe variable isn't set in this terminal, or the name differsSet it as in Step 2 and compare the name
A number from the environment won't add upEvery value is a stringConvert it, for example with int.Parse

FAQ​

Are variable names case-sensitive?​

It depends on the system. The API documentation says: "Environment variable names are case-sensitive on Unix-like systems but aren't case-sensitive on Windows."

What about ASP.NET Core apps?​

Their configuration system reads environment variables for you, alongside appsettings.json. Environment.GetEnvironmentVariable is the direct way for small programs and scripts.

Conclusion​

Environment.GetEnvironmentVariable("NAME") returns a variable's value as a string, or null when it isn't set. Check for null, and match the name's case on macOS and Linux.

Additional Resources​