dotenv.net 4.0.0

dotnet add package dotenv.net --version 4.0.0
                    
NuGet\Install-Package dotenv.net -Version 4.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="dotenv.net" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="dotenv.net" Version="4.0.0" />
                    
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.0
                    
#r "nuget: dotenv.net, 4.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.
#:package dotenv.net@4.0.0
                    
#: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.0
                    
Install as a Cake Addin
#tool nuget:?package=dotenv.net&version=4.0.0
                    
Install as a Cake Tool

dotenv.net ๐ŸŒŸ

CI - Build, Test & Coverage License: MIT Coverage Status NuGet Version

project icon

dotenv.net is a lightweight and intuitive library designed to simplify the process of managing environment variables in .NET applications. By seamlessly integrating with .env files, it allows developers to maintain a clean and secure configuration setup, ensuring that sensitive information is kept out of source code. ๐Ÿ”’

Whether you're building a small project or a large-scale application, dotenv.net provides the tools you need to efficiently load, read, and manage environment variables, with support for dependency injection (DI) in popular DI systems. ๐Ÿ› ๏ธ


Why Use dotenv.net? ๐Ÿค”

  • Simple and Pain-Free ๐ŸŽฏ: Easily load and read .env files with minimal setup.
  • Flexible Configuration ๐Ÿ”ง: Customize how environment variables are loaded with a variety of options.
  • Dependency Injection Support ๐Ÿงฉ: Works seamlessly with popular DI frameworks.
  • Cross-Platform ๐ŸŒ: Fully compatible with .NET Core, .NET 5, and beyond.
  • Open Source ๐Ÿ’ก: Actively maintained and supported by the community.

Getting Started ๐Ÿš€

Installation ๐Ÿ“ฆ

You can install dotenv.net via NuGet:

  • Using the .NET CLI:

    dotnet add package dotenv.net
    
  • Using Visual Studio Package Manager:

    Install-Package dotenv.net
    
  • Manual Installation (via .csproj):

    <PackageReference Include="dotenv.net" Version="4.0.0"/>
    

Usage ๐Ÿ› ๏ธ

Basic Setup ๐Ÿ—๏ธ

  1. Add the Namespace:

    using dotenv.net;
    
  2. Load Environment Variables:

    DotEnv.Load();
    

    This will automatically locate and load the .env file in the same directory as your application.


Advanced Configuration โš™๏ธ

dotenv.net offers a wide range of configuration options to tailor the loading process to your needs:

  • Specify Custom .env File Paths:

    DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] {"./path/to/env", "./path/to/second/env"}));
    
  • Enable Exception Handling:

    DotEnv.Load(options: new DotEnvOptions(ignoreExceptions: false));
    
  • Search for .env Files in Parent Directories:

    DotEnv.Load(options: new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 2));
    
  • Trim Whitespace from Values:

    DotEnv.Load(options: new DotEnvOptions(trimValues: true));
    
  • Skip Overwriting Existing Environment Variables:

    DotEnv.Load(options: new DotEnvOptions(overwriteExistingVars: false));
    
  • Support the Export Syntax:

    DotEnv.Load(options: new DotEnvOptions(supportExportSyntax: true));
    

Reading Environment Variables ๐Ÿ“–

Use the Read() method to retrieve environment variables without modifying the system environment:

var envVars = DotEnv.Read();
Console.WriteLine(envVars["KEY"]); // Outputs the value associated with 'KEY'

Fluent API ๐ŸŽจ

For a more expressive syntax, dotenv.net provides a fluent API:

// Load environment variables with custom options
DotEnv.Fluent()
    .WithExceptions()
    .WithEnvFiles("./path/to/env")
    .WithTrimValues()
    .WithEncoding(Encoding.ASCII)
    .WithOverwriteExistingVars()
    .WithProbeForEnv(probeLevelsToSearch: 6)
    .WithSupportExportSyntax()
    .Load();

// Read environment variables
var envVars = DotEnv.Fluent()
    .WithoutExceptions()
    .WithEnvFiles() // Defaults to .env
    .WithoutTrimValues()
    .WithDefaultEncoding()
    .WithoutOverwriteExistingVars()
    .WithoutProbeForEnv()
    .WithoutSupportExportSyntax()
    .Read();

Environment Variable Helpers ๐Ÿ› ๏ธ

The Utilities namespace provides additional methods for reading environment variables in a typed manner:

using dotenv.net.Utilities;

var stringValue = EnvReader.GetStringValue("KEY");
var intValue = EnvReader.GetIntValue("PORT");
var boolValue = EnvReader.GetBooleanValue("ENABLE_FEATURE");
Available Methods ๐Ÿ“‹
Method Name Description Return Type Default (if applicable)
HasValue(string key) Checks if a value is set for the given key. bool N/A
GetStringValue(string key) Retrieves a string value by key. Throws an exception if not found. string N/A
GetIntValue(string key) Retrieves an integer value by key. Throws an exception if not found. int N/A
GetDoubleValue(string key) Retrieves a double value by key. Throws an exception if not found. double N/A
GetDecimalValue(string key) Retrieves a decimal value by key. Throws an exception if not found. decimal N/A
GetBooleanValue(string key) Retrieves a boolean value by key. Throws an exception if not found. bool N/A
TryGetStringValue(string key, out string value) Safely retrieves a string value. Returns true if successful. bool null
TryGetIntValue(string key, out int value) Safely retrieves an integer value. Returns true if successful. bool 0
TryGetDoubleValue(string key, out double value) Safely retrieves a double value. Returns true if successful. bool 0.0
TryGetDecimalValue(string key, out decimal value) Safely retrieves a decimal value. Returns true if successful. bool 0.0m
TryGetBooleanValue(string key, out bool value) Safely retrieves a boolean value. Returns true if successful. bool false

Contributing ๐Ÿค

We welcome contributions from the community! If you have ideas, bug reports, or feature requests, please open an issue or submit a pull request.

Special Thanks to Our Contributors ๐Ÿ™

A huge shoutout 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 more details.


Get Started Today! ๐ŸŽ‰

Simplify your environment variable management with dotenv.net. Install the package, follow the quick start guide, and enjoy a cleaner, more secure configuration setup for your .NET applications.

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

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

CasAuth

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

Injector

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

FluentConsole.Templates

ๆไพ›ใ€Œ็ŽฐไปฃๅŒ–็š„ๆŽงๅˆถๅฐๅบ”็”จ็š„ๅผ€ๅ‘ไฝ“้ชŒใ€่„šๆ‰‹ๆžถ๏ผŒ่ƒฝๅƒ Web ๅบ”็”จ้‚ฃๆ ทๅพˆไผ˜้›…ๅœฐๆ•ดๅˆๅ„็ง็ป„ไปถ๏ผŒๅŒ…ๆ‹ฌไพ่ต–ๆณจๅ…ฅใ€้…็ฝฎใ€ๆ—ฅๅฟ—็ญ‰ๅŠŸ่ƒฝใ€‚

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on dotenv.net:

Repository Stars
OpenCoreMMO/OpenCoreMMO
Modern MMORPG server emulator written in C#
redis/NRedisStack
Redis Stack .Net client
Azure/azure-sdk-tools
Tools repository leveraged by the Azure SDK team.
Version Downloads Last Updated
4.0.0 4,761 7/11/2025
3.2.1 722,863 9/21/2024
3.2.0 297,508 6/22/2024
3.1.3 806,005 11/5/2023
3.1.2 2,560,946 11/25/2022
3.1.1 783,256 10/11/2021
3.1.0 119,014 7/11/2021
3.0.0 453,780 3/19/2021
2.1.3 221,883 1/18/2021
2.1.1 186,621 5/26/2020
2.1.0 132,312 4/1/2020
2.0.1 2,000 3/27/2020
2.0.0 1,631 3/25/2020
1.0.6 412,073 6/29/2019
1.0.5 1,406 6/27/2019
1.0.4 121,406 10/21/2018
1.0.3 26,910 2/17/2018
1.0.2 1,845 1/15/2018
1.0.1 2,035 12/31/2017
1.0.0 9,878 11/22/2017

- support quotation escape support