.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package --version 2.0.0
NuGet\Install-Package -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add --version 2.0.0
#r "nuget:, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install as a Cake Addin
#addin nuget:?

// Install as a Cake Tool
#tool nuget:?

CircleCI NuGet Badge License: MIT Coverage Status is a zero-dependency module that loads environment variables from a .env environment variable file into System.Environment. It has built in support for the in-built dependency injection framework packaged with ASP.NET Core. It now comes packaged with an interface that allows for reading environment variables wihtout repeated calls to Environment.GetEnvironmentVariable("KEY");. If you have ideas or issues, create an issue.


Big ups to those who have contributed to this library. 👏

@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill @texyh



First install the library as a dependency in your application from nuget



dotnet add package

or for paket

paket add

Create a file with no filename and an extension of .env.

A sample .env file would look like this:


in the Startup.cs file or as early as possible in your code add the following:




the values saved in your .env file would be avaibale in your application and can be accessed via

Environment.GetEnvironmentVariable("DB_HOST"); // would output 'localhost'

Using with DI (IServiceCollection)

If using with ASP.NET Core or any other system that uses IServiceCollection for its dependency injection, in the Startup.cs file

public void ConfigureServices(IServiceCollection services)

    // configure dotenv
    services.AddEnv(builder => {

With this, your application would have the env file variables imported.



Default: true

You can specify if you want the library to error out if any issue arises or fail silently.

DotEnv.Config(false); //fails silently

Default: .env

You can specify a custom path if your file containing environment variables is named or located differently.

DotEnv.Config(true, "/custom/path/to/your/env/vars");

Default: Encoding.UTF8

You may specify the encoding of your file containing environment variables using this option.

DotEnv.Config(true, ".env", Encoding.Unicode);
Trim Values

Default: true

You may specify whether or not you want the values retrieved to be trimmed i.e have all leading and trailing whitepaces removed.

DotEnv.Config(true, ".env", Encoding.Unicode, false);

Support For IEnvReader

With v1.0.6 and above an interface IEnvReader has been introduced that specifies methods that help with reading values from the environment easily. The library has a default implementation EnvReader that can be added to the default ASP.NET Core DI framework (IServiceCollection).

Using EnvReader


var envReader = new EnvReader();
var value = envReader.GetValue("KEY");

Using IEnvReader with DI

In the StartUp.cs file, in the ConfigureServices method


public void ConfigureServices(IServiceCollection services)

In the rest of your application, the IEnvReader interface can get injected and used. For example, in a SampleController class for example:

public class SampleController
    private readonly IEnvReader _envReader;
    public SampleController(IEnvReader envReader)
        _envReader = envReader;

IEnvReader Methods

string GetValue(string key)

Default: null

Retrieve a value from the current environment by the given key and return null if a value does not exist for that key.

T GetValue<T>(string key)

Default: default(T)

A generic method that allows for a typed value to be retrieved from the environment variables and returns the default for the type. This functionality is limited to structs currently.

bool TryGetValue(string key, out string value)

Default: null

A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required.

bool TryGetValue<T>(string key, out T value)

Default: default(T)

A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved and coverted the value required.

Product 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. 
.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. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (14)

Showing the top 5 NuGet packages that depend on

Package Downloads

Adds DI support for values read from the environment for Microsoft Service Collection


Commonly used tooling for dotnetcore, including configuration management, logging, etc.


CodeZero is a set of common implementations to help you implementing Clean Architecture, DDD, CQRS, Specification Patterns and another facilities for new modern web applications is an open-source project written in .NET Core.


Injects values into config files directly or via environment variables. Can inject app settings, connection strings, or WCF client endpoints.


The Comprehensive Authentication Solution (or CasAuth) was developed to provide an opinionated way to handle user and service authentication for APIs.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on

Repository Stars
Carbon-Aware SDK
Tools repository leveraged by the Azure SDK team.
Version Downloads Last updated
3.1.2 208,901 11/25/2022
3.1.1 518,227 10/11/2021
3.1.0 82,500 7/11/2021
3.0.0 213,847 3/19/2021
2.1.3 178,821 1/18/2021
2.1.1 157,297 5/26/2020
2.1.0 98,833 4/1/2020
2.0.1 1,289 3/27/2020
2.0.0 513 3/25/2020
1.0.6 361,146 6/29/2019
1.0.5 694 6/27/2019
1.0.4 95,130 10/21/2018
1.0.3 23,012 2/17/2018
1.0.2 1,054 1/15/2018
1.0.1 1,105 12/31/2017
1.0.0 4,166 11/22/2017

- Add support for IoC with AutoFac.
- Re-engineer the env reader
- Update tests and improve coverage
- Support values surrounded by quotes