dotenv.net 4.0.2

dotnet add package dotenv.net --version 4.0.2
                    
NuGet\Install-Package dotenv.net -Version 4.0.2
                    
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="dotenv.net" Version="4.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="dotenv.net" Version="4.0.2" />
                    
Directory.Packages.props
<PackageReference Include="dotenv.net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add dotenv.net --version 4.0.2
                    
#r "nuget: dotenv.net, 4.0.2"
                    
#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.
#:package dotenv.net@4.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=dotenv.net&version=4.0.2
                    
Install as a Cake Addin
#tool nuget:?package=dotenv.net&version=4.0.2
                    
Install as a Cake Tool

dotenv.net

Build, Test & Coverage License: MIT codecov NuGet Version

project icon

dotenv.net is a lightweight library for loading environment variables from .env files in .NET applications. It keeps sensitive configuration out of source code and supports a range of options to suit different project setups.

Installation

Install via NuGet using any of the following methods:

.NET CLI

dotnet add package dotenv.net

Package Manager Console

Install-Package dotenv.net

PackageReference


<PackageReference Include="dotenv.net" Version="4.x.x"/>

Quick Start

using dotenv.net;

// Load .env from the application directory into the system environment
DotEnv.Load();

// Read variables from .env without modifying the system environment
var envVars = DotEnv.Read();
Console.WriteLine(envVars["DATABASE_URL"]);

By default, both methods look for a .env file in the same directory as the application executable.

Configuration Options

Both Load() and Read() accept a DotEnvOptions instance to customise behaviour.

DotEnv.Load(options: new DotEnvOptions(
    ignoreExceptions: false,           // Throw on errors instead of silently failing (default: true)
    envFilePaths: ["./config/.env"],   // One or more paths to .env files (default: [".env"])
    encoding: Encoding.UTF8,           // File encoding (default: UTF-8)
    trimValues: true,                  // Strip whitespace from values (default: false)
    overwriteExistingVars: false,      // Skip vars already set in the environment (default: true)
    probeForEnv: true,                 // Search parent directories for a .env file (default: false)
    probeLevelsToSearch: 3,            // How many directory levels to ascend when probing (default: 4)
    supportExportSyntax: true          // Support `export KEY=VALUE` syntax (default: false)
));

Note: probeForEnv and envFilePaths are mutually exclusive. Setting both will throw an InvalidOperationException.

Loading multiple .env files

DotEnv.Load(options: new DotEnvOptions(
    envFilePaths: ["./config/.env", "./secrets/.env"]
));

When overwriteExistingVars is false, keys from earlier files take precedence over those in later files.

Fluent API

DotEnv.Fluent() returns a DotEnvOptions instance that exposes a chainable builder API, useful when you prefer an explicit, readable configuration style.

Loading variables:

DotEnv.Fluent()
    .WithExceptions()
    .WithEnvFiles("./config/.env")
    .WithTrimValues()
    .WithEncoding(Encoding.UTF8)
    .WithOverwriteExistingVars()
    .WithProbeForEnv(probeLevelsToSearch: 6)
    .WithSupportExportSyntax()
    .Load();

Reading variables without writing to the environment:

var envVars = DotEnv.Fluent()
    .WithoutExceptions()
    .WithEnvFiles()               // Defaults to .env
    .WithoutTrimValues()
    .WithEncoding(Encoding.UTF8)
    .WithoutOverwriteExistingVars()
    .WithoutProbeForEnv()
    .WithoutSupportExportSyntax()
    .Read();

Fluent builder methods

Method Description
WithExceptions() Throw exceptions on errors
WithoutExceptions() Silently ignore errors (default)
WithEnvFiles(params string[]) Specify one or more .env file paths
WithEncoding(Encoding) Set file encoding
WithTrimValues() Strip whitespace from values
WithoutTrimValues() Preserve whitespace in values (default)
WithOverwriteExistingVars() Overwrite existing environment variables (default)
WithoutOverwriteExistingVars() Preserve existing environment variables
WithProbeForEnv(int) Search parent directories for a .env file
WithoutProbeForEnv() Disable parent directory search (default)
WithSupportExportSyntax() Support export KEY=VALUE syntax
WithoutSupportExportSyntax() Disable export syntax support (default)

Reading Variables

DotEnv.Read() returns an IDictionary<string, string> of the parsed key-value pairs without writing anything to the system environment. This is useful for inspecting values or selectively applying them.

var envVars = DotEnv.Read();

if (envVars.TryGetValue("API_KEY", out var apiKey))
{
    // use apiKey
}

EnvReader Utility

The dotenv.net.Utilities namespace provides EnvReader, a helper class for reading strongly-typed values directly from the system environment (i.e., after calling DotEnv.Load()).

using dotenv.net.Utilities;

var host = EnvReader.GetStringValue("DB_HOST");
var port = EnvReader.GetIntValue("DB_PORT");
var enabled = EnvReader.GetBooleanValue("FEATURE_FLAG");

For non-throwing alternatives, use the TryGet* methods:

if (EnvReader.TryGetIntValue("DB_PORT", out var port))
{
    // port is valid
}

Available methods

Method Return Type Throws if missing
HasValue(string key) bool No
GetStringValue(string key) string Yes
GetIntValue(string key) int Yes
GetDoubleValue(string key) double Yes
GetDecimalValue(string key) decimal Yes
GetBooleanValue(string key) bool Yes
TryGetStringValue(string key, out string value) bool No, returns null on failure
TryGetIntValue(string key, out int value) bool No, returns 0 on failure
TryGetDoubleValue(string key, out double value) bool No, returns 0.0 on failure
TryGetDecimalValue(string key, out decimal value) bool No, returns 0.0m on failure
TryGetBooleanValue(string key, out bool value) bool No, returns false on failure

Contributing

Contributions are welcome. If you have a bug report, feature request, or improvement in mind, please open an issue or submit a pull request.

To get started:

  1. Fork the repository and create a feature branch.
  2. Make your changes and ensure existing tests still pass.
  3. Add tests for any new behaviour.
  4. Open a pull request with a clear description of what was changed and why.

The project targets .NET and uses the standard dotnet CLI toolchain. Run the test suite with:

dotnet test

Contributors

Thanks to everyone who has contributed to dotenv.net:

@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill @texyh @jonlabelle @Gounlaf @DTTerastar @Mondonno @caveman-dick @VijoPlays @bobbyg603

License

dotenv.net is licensed under the MIT License. See the LICENSE file for details.

Happy Coding! 🚀

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.  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 is compatible. 
.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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • .NETStandard 2.1

    • No dependencies.

NuGet packages (22)

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 (6)

Showing the top 6 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
Version Downloads Last Updated
4.0.2 12,440 4/17/2026
4.0.1 125,602 2/25/2026
4.0.0 768,564 7/11/2025
3.2.1 1,483,727 9/21/2024
3.2.0 391,497 6/22/2024
3.1.3 1,002,161 11/5/2023
3.1.2 3,513,742 11/25/2022
3.1.1 809,685 10/11/2021
3.1.0 129,160 7/11/2021
3.0.0 725,715 3/19/2021
2.1.3 237,406 1/18/2021
2.1.1 191,967 5/26/2020
2.1.0 139,771 4/1/2020
2.0.1 3,152 3/27/2020
2.0.0 3,798 3/25/2020
1.0.6 422,456 6/29/2019
1.0.5 2,502 6/27/2019
1.0.4 124,516 10/21/2018
1.0.3 28,243 2/17/2018
Loading failed

- add support for reading from streams
- improve README