dotenv.net
2.0.1
See the version list below for details.
dotnet add package dotenv.net --version 2.0.1
NuGet\Install-Package dotenv.net -Version 2.0.1
<PackageReference Include="dotenv.net" Version="2.0.1" />
<PackageVersion Include="dotenv.net" Version="2.0.1" />
<PackageReference Include="dotenv.net" />
paket add dotenv.net --version 2.0.1
#r "nuget: dotenv.net, 2.0.1"
#:package dotenv.net@2.0.1
#addin nuget:?package=dotenv.net&version=2.0.1
#tool nuget:?package=dotenv.net&version=2.0.1
dotenv.net
dotenv.net 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.
Contributors
Big ups to those who have contributed to this library. 👏
@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill @texyh
Usage
Conventional
First install the library as a dependency in your application from nuget
Install-Package dotenv.net
or
dotnet add package dotenv.net
or for paket
paket add dotenv.net
Create a file with no filename and an extension of .env.
A sample .env file would look like this:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
in the Startup.cs file or as early as possible in your code add the following:
using dotenv.net;
...
DotEnv.Config();
At times the .env would not be in the same directory as your assembly, in that case the following would apply
using dotenv.net
...
var success = DotEnv.AutoConfig(); // success holds a value stating whether the env was found and read or not
this looks through your assembly's folder and three folders up for a .env file and loads that.
the values saved in your .env file would be availale 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 => {
builder
.AddEnvFile("/custom/path/to/your/env/vars")
.AddThrowOnError(false)
.AddEncoding(Encoding.ASCII);
});
}
Using with DI (ContainerBuilder)
If using with ASP.NET Core or any other system that uses ContainerBuilder for its dependency injection, in the Startup.cs file
...
// configure dotenv
containerBuilder.AddEnv(builder => {
builder
.AddEnvFile("/custom/path/to/your/env/vars")
.AddThrowOnError(false)
.AddEncoding(Encoding.ASCII);
});
With any of the above, your application would have the env file variables imported.
Options
ThrowError
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
Path
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");
Encoding
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
using dotenv.net.Utilities;
...
var envReader = new EnvReader();
var value = envReader.GetValue("KEY");
Using IEnvReader with DI
In the StartUp.cs file, in the ConfigureServices method
For (ASP.NET DI)
public void ConfigureServices(IServiceCollection services)
{
...
services.AddEnvReader();
...
}
For Autofac
...
containerBuilder.AddEnvReader();
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 GetStringValue(string key)
Default: null
Retrieve a value from the current environment by the given key and throws an exception if not found.
int GetIntValue(string key)
Default: 0
Retrieve a value from the current environment by the given key and throws an exception if not found.
double GetDoubleValue(string key)
Default: 0.0
Retrieve a value from the current environment by the given key and throws an exception if not found.
decimal GetDecimalValue(string key)
Default: 0.0m
Retrieve a value from the current environment by the given key and throws an exception if not found.
bool GetBooleanValue(string key)
Default: false
Retrieve a value from the current environment by the given key and throws an exception if not found.
bool TryGetStringValue(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 TryGetIntValue(string key, out int value)
Default: 0
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 TryGetDoubleValue(string key, out double value)
Default: 0.0
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 TryGetDecimalValue(string key, out decimal value)
Default: 0.0m
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 TryGetBooleanValue(string key, out bool value)
Default: false
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.
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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
- Autofac (>= 5.1.2)
- Microsoft.Extensions.DependencyInjection (>= 3.1.3)
- System.Memory (>= 4.5.4)
NuGet packages (23)
Showing the top 5 NuGet packages that depend on dotenv.net:
| Package | Downloads |
|---|---|
|
CodeZero
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. |
|
|
MCMS.Base
MCMS Base package |
|
|
Injector
Injects values into config files directly or via environment variables. Can inject app settings, connection strings, or WCF client endpoints. |
|
|
CasAuth
The Comprehensive Authentication Solution (or CasAuth) was developed to provide an opinionated way to handle user and service authentication for APIs. |
|
|
dotenv-vault.net
dotnet lib for integrating dotenv-vault |
GitHub repositories (7)
Showing the top 7 popular GitHub repositories that depend on dotenv.net:
| Repository | Stars |
|---|---|
|
Deali-Axy/StarBlog
☀StarBlog 是一个基于 .NET Core 开发的现代博客系统,支持 Markdown 文章导入,遵循 RESTful 接口规范。前端基于 Vue + ElementUI 开发,可作为 .NET Core 入门学习项目,同时配套了一系列开发笔记,记录了从零开始构建这个博客系统的全过程,可以帮助学习理解 .Net Core 项目的开发流程。
|
|
|
redis/NRedisStack
Redis Stack .Net client
|
|
|
Azure-Samples/azure-search-dotnet-samples
Azure Search .NET sample code
|
|
|
Azure/azure-sdk-tools
Tools repository leveraged by the Azure SDK team.
|
|
|
codez0mb1e/BinanceBot
Market Maker Bot for Binance
|
|
|
DaredevilOSS/sqlc-gen-csharp
|
|
|
manasseh-zw/apollo
Apollo ~ Deep Research Meta Agent
|
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.2 | 40,413 | 4/17/2026 |
| 4.0.1 | 147,181 | 2/25/2026 |
| 4.0.0 | 810,757 | 7/11/2025 |
| 3.2.1 | 1,527,856 | 9/21/2024 |
| 3.2.0 | 396,722 | 6/22/2024 |
| 3.1.3 | 1,013,258 | 11/5/2023 |
| 3.1.2 | 3,573,288 | 11/25/2022 |
| 3.1.1 | 812,225 | 10/11/2021 |
| 3.1.0 | 129,423 | 7/11/2021 |
| 3.0.0 | 742,337 | 3/19/2021 |
| 2.1.3 | 238,311 | 1/18/2021 |
| 2.1.1 | 192,214 | 5/26/2020 |
| 2.1.0 | 139,934 | 4/1/2020 |
| 2.0.1 | 3,172 | 3/27/2020 |
| 2.0.0 | 3,980 | 3/25/2020 |
| 1.0.6 | 423,494 | 6/29/2019 |
| 1.0.5 | 2,520 | 6/27/2019 |
| 1.0.4 | 124,640 | 10/21/2018 |
| 1.0.3 | 28,263 | 2/17/2018 |
- Add auto config support
- Adds AutoFac support for the env reader IoC