DotNetEnv 1.2.0

.NET Standard 1.3
There is a newer version of this package available.
See the version list below for details.
Install-Package DotNetEnv -Version 1.2.0
dotnet add package DotNetEnv --version 1.2.0
<PackageReference Include="DotNetEnv" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetEnv --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DotNetEnv, 1.2.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install DotNetEnv as a Cake Addin
#addin nuget:?package=DotNetEnv&version=1.2.0

// Install DotNetEnv as a Cake Tool
#tool nuget:?package=DotNetEnv&version=1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Windows build status License: MIT NuGet version

dotnet-env

A .NET library to load environment variables from .env files. Supports .NET Core and .NET Framework

Installation

Available on NuGet

Visual Studio:

PM> Install-Package DotNetEnv

.NET Core CLI:

dotnet add package DotNetEnv

Usage

Load env file

Load() will automatically look for a .env file in the current directory

DotNetEnv.Env.Load();

Or you can specify the path to the .env file

DotNetEnv.Env.Load("./path/to/.env");

It's also possible to load the (text) file as a Stream

using (var stream = File.OpenRead("./path/to/.env"))
{
    DotNetEnv.Env.Load(stream);
}

Accessing environment variables

The variables in the .env can then be accessed through the System.Environment class

System.Environment.GetEnvironmentVariable("IP");

Or through on of the helper methods:

DotNetEnv.Env.GetString("A_STRING");
DotNetEnv.Env.GetBool("A_BOOL");
DotNetEnv.Env.GetInt("AN_INT");
DotNetEnv.Env.GetDouble("A_DOUBLE");

The helper methods also has a optional second argument which specifies what value to return if the variable is not found:

DotNetEnv.Env.GetString("THIS_DOES_NOT_EXIST", "Variable not found");

Additional arguments

You can also control whitespace trimming and allowing hashes in values

DotNetEnv.Env.Load(false, false);

Both parameters default to true, which means:

  1. trimWhitespace, first arg: true in order to trim leading and trailing whitespace from keys and values such that
  KEY  =  value

Would then be available as

"value" == System.Environment.GetEnvironmentVariable("KEY")
null == System.Environment.GetEnvironmentVariable("  KEY  ")

False would mean:

"  value" == System.Environment.GetEnvironmentVariable("  KEY  ")
null == System.Environment.GetEnvironmentVariable("KEY")
  1. isEmbeddedHashComment, second arg: true in order to allow inline comments
KEY=value  # comment

Would then be available as

"value" == System.Environment.GetEnvironmentVariable("KEY")

False would mean:

"value  # comment" == System.Environment.GetEnvironmentVariable("KEY")

Which is most useful when you want to do something like:

KEY=value#moreValue#otherValue#etc
  1. unescapeQuotedValues, third arg: true in order to unescape/parse quoted (single or double) values as being strings with escaped chars such as newline ("\n"), but also handles unicode chars (e.g. "\u00ae" and "\U0001F680") -- note that you can always include unescaped unicode chars anyway (e.g. "日本") if your .env is in UTF-8. Also note that there is no need to escape quotes inside.
KEY="quoted\n\tvalue"

Would then be available as

"quoted
    value" == System.Environment.GetEnvironmentVariable("KEY")

False would mean:

"\"quoted\\n\\tvalue\"" == System.Environment.GetEnvironmentVariable("KEY")
  1. clobberExistingVars, fourth arg: false to avoid overwriting existing environment variables
KEY=value
System.Environment.SetEnvironmentVariable("KEY", "really important value, don't overwrite");
DotNetEnv.Env.Load(false, false, false, false); // fourth arg false, don't overwrite existing variables
System.Environment.GetEnvironmentVariable("KEY"); // == "really important value, don't overwrite"

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on DotNetEnv:

Package Downloads
Corgibytes.Freshli.Lib

Core library for collecting historical metrics about a project's dependencies

Rave.NET

Flutterwave RAVE Payment API Library for .NET.

hexasync.infrastructure.dotnetenv

Package Description

CatConsult.Extensions.EnvFileConfigurationProvider

Package Description

Kanyon.Engine

Kanyon.Engine provides the classes needed to generate YAML from Kanyon manifests

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on DotNetEnv:

Repository Stars
mehdihadeli/ecommerce-microservices
🛍️ A practical e-commerce microservices sample, built with C#, .Net Core, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
Version Downloads Last updated
2.3.0 264,867 1/15/2022
2.2.0 208,134 9/18/2021
2.1.1 359,427 3/2/2021
2.1.0 7,125 2/22/2021
2.0.0 85,180 12/16/2020
1.4.0 577,190 12/9/2019
1.3.1 508 12/5/2019
1.2.0 409,260 4/5/2018
1.1.0 9,148 11/2/2017
1.0.1 3,768 6/18/2017
1.0.0 2,529 7/7/2016