How To Read Environment Variables In C# & .NET
[C#, .NET]
Our previous post, “How To Read Environment Variables In PowerShell”, looked at how you can read environment variables using PowerShell and either just view them or use them subsequently in your scripts.
In this post, we will look at how to do the same in C# & .NET.
The Environment class has a method, GetEnvironmentVariable(), for this purpose. This returns an IDictionary collection we can iterate over.
Console.WriteLine(Environment.GetEnvironmentVariable("COMMAND_MODE"));
This will print what we are expecting:

It is also possible to get all the environment variables via the appropriately named GetEnvironmentVariables() method.
var entries = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry entry in entries)
{
Console.WriteLine($"Name: {entry.Key}; Values: {entry.Value}");
}
This will print something like this:

TLDR
You can retrieve environment variables in C# using either GetEnvironmentVariable() or GetEnvironmentVariables() methods from the Environment object.
Happy hacking!