DynamicEnv 1.0.3
dotnet add package DynamicEnv --version 1.0.3
NuGet\Install-Package DynamicEnv -Version 1.0.3
<PackageReference Include="DynamicEnv" Version="1.0.3" />
paket add DynamicEnv --version 1.0.3
#r "nuget: DynamicEnv, 1.0.3"
// Install DynamicEnv as a Cake Addin #addin nuget:?package=DynamicEnv&version=1.0.3 // Install DynamicEnv as a Cake Tool #tool nuget:?package=DynamicEnv&version=1.0.3
DynamicEnv
.NET Standard 2.0 library that exposes environment variable interactions through a Dynamic object.
Getting Started
Install DynamicEnv
from NuGet. E.g. from Visual Studio's Package Manager Console:
Install-Package DynamicEnv
Usage
You can then use the Env
class to access environment variables succinctly via a dynamic
object. You can use either dynamic member syntax or indexes (similar to a string dictionary, although this class does not currently implement any dictionary interface).
First you need an instance of Env
:
using DynamicEnv;
//...
dynamic env = new Env();
//Or explicitly specify the environment target (the default when none is
//specified, like the line above, is EnvironmentVariableTarget.Process)
env = new Env(EnvironmentVariableTarget.Process)
//Or you can use one of the pre-initialized static instances:
env = Env.Machine; //Targets machine-level environment variables (Windows systems only)
env = Env.User; //Targets user-level environment variables (Windows systems only)
env = Env.Process; //Targets process-level environment variables (this is the default)
Read an environment variable; note, their names may be case sensitive on some platforms (Linux/Mac):
string path;
path = env.PATH;
path = env["PATH"];
If the environment variable doesn't exist, then it'll return a null
:
string empty;
empty = env.ThisVarDoesntExist;
empty = env["ThisVarDoesntExist"];
Assert.True(empty == null);
You can also set environment variables like this:
env.DataRoot = @"C:\Data";
env["DataRoot"] = @"C:\Data";
Deleting/clearing an environment variable is typically done by setting as null
:
env.DataRoot = null;
env["DataRoot"] = null;
Enumerate environment variable names:
IEnumerable<string> varNames = env.GetDynamicMemberNames();
foreach (string name in varNames) {
string val = env[name];
Console.WriteLine($"{name}: {val}");
}
Errors
An unsupported value type (currently anything other than String
) throws an ArgumentException
:
env.Test = 454;
An unsupported index type (currently anything other than String
) throws an IndexOutOfRangeException
:
env[12] = "this won't work!";
And finally here's a gotcha that causes a RuntimeBinderException
because .NET can't determine whether the null
result is a string
or a char[]
parameter to the Console.WriteLine(...)
method:
Console.WriteLine(env.ThisVarDoesntExist);
You can work around this by casting to string
:
Console.WriteLine($"{env.ThisVarDoesntExist}");
Console.WriteLine((string)env.ThisVarDoesntExist);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.3 | 193 | 8/10/2023 |
1.0.3-ci0002 | 106 | 8/10/2023 |
1.0.3-ci0001 | 105 | 8/10/2023 |
1.0.2 | 132 | 8/10/2023 |
1.0.2-ci0006 | 121 | 8/10/2023 |
1.0.1 | 501 | 4/23/2020 |
1.0.0 | 439 | 4/22/2020 |