![]() |
VOOZH | about |
dotnet add package dotenv.net --version 4.0.2
NuGet\Install-Package dotenv.net -Version 4.0.2
<PackageReference Include="dotenv.net" Version="4.0.2" />
<PackageVersion Include="dotenv.net" Version="4.0.2" />Directory.Packages.props
<PackageReference Include="dotenv.net" />Project file
paket add dotenv.net --version 4.0.2
#r "nuget: dotenv.net, 4.0.2"
#:package dotenv.net@4.0.2
#addin nuget:?package=dotenv.net&version=4.0.2Install as a Cake Addin
#tool nuget:?package=dotenv.net&version=4.0.2Install as a Cake Tool
👁 Build, Test & Coverage
👁 codecov
👁 NuGet Version
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.
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"/>
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.
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:
probeForEnvandenvFilePathsare mutually exclusive. Setting both will throw anInvalidOperationException.
.env filesDotEnv.Load(options: new DotEnvOptions(
envFilePaths: ["./config/.env", "./secrets/.env"]
));
When overwriteExistingVars is false, keys from earlier files take precedence over those in later files.
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();
| 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) |
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
}
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
}
| 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 |
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:
The project targets .NET and uses the standard dotnet CLI toolchain. Run the test suite with:
dotnet test
Thanks to everyone who has contributed to dotenv.net:
@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill @texyh @jonlabelle @Gounlaf @DTTerastar @Mondonno @caveman-dick @VijoPlays @bobbyg603
dotenv.net is licensed under the MIT License. See the file for details.
Happy Coding! 🚀
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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 |
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 | 111,914 | 4/17/2026 |
| 4.0.1 | 185,101 | 2/25/2026 |
| 4.0.0 | 872,801 | 7/11/2025 |
| 3.2.1 | 1,608,015 | 9/21/2024 |
| 3.2.0 | 408,390 | 6/22/2024 |
| 3.1.3 | 1,036,378 | 11/5/2023 |
| 3.1.2 | 3,707,249 | 11/25/2022 |
| 3.1.1 | 815,569 | 10/11/2021 |
| 3.1.0 | 130,097 | 7/11/2021 |
| 3.0.0 | 774,414 | 3/19/2021 |
| 2.1.3 | 240,170 | 1/18/2021 |
| 2.1.1 | 192,642 | 5/26/2020 |
| 2.1.0 | 140,338 | 4/1/2020 |
| 2.0.1 | 3,271 | 3/27/2020 |
| 2.0.0 | 4,506 | 3/25/2020 |
| 1.0.6 | 424,895 | 6/29/2019 |
| 1.0.5 | 2,639 | 6/27/2019 |
| 1.0.4 | 125,018 | 10/21/2018 |
| 1.0.3 | 28,522 | 2/17/2018 |
- add support for reading from streams
- improve README