A .NET library to load environment variables from .env files
Switch branches/tags
Clone or download
Latest commit 27ee671 Apr 5, 2018


Windows build status License: MIT NuGet version


A .NET library to load environment variables from .env files. Supports .NET Core and .NET Framework


Available on NuGet

Visual Studio:

PM> Install-Package DotNetEnv

.NET Core CLI:

dotnet add package DotNetEnv


Load env file

Load() will automatically look for a .env file in the current directory


Or you can specify the path to the .env file


It's also possible to load the (text) file as a Stream

using (var stream = File.OpenRead("./path/to/.env"))

Accessing environment variables

The variables in the .env can then be accessed through the System.Environment class


Or through on of the helper methods:


The helper methods also has a optional second argument which specifies what value to return if the variable is not found:

DotNetEnv.Env.GetString("THIS_DOES_NOT_EXIST", "Variable not found");

Additional arguments

You can also control whitespace trimming and allowing hashes in values

DotNetEnv.Env.Load(false, false);

Both parameters default to true, which means:

  1. trimWhitespace, first arg: true in order to trim leading and trailing whitespace from keys and values such that
  KEY  =  value

Would then be available as

"value" == System.Environment.GetEnvironmentVariable("KEY")
null == System.Environment.GetEnvironmentVariable("  KEY  ")

False would mean:

"  value" == System.Environment.GetEnvironmentVariable("  KEY  ")
null == System.Environment.GetEnvironmentVariable("KEY")
  1. isEmbeddedHashComment, second arg: true in order to allow inline comments
KEY=value  # comment

Would then be available as

"value" == System.Environment.GetEnvironmentVariable("KEY")

False would mean:

"value  # comment" == System.Environment.GetEnvironmentVariable("KEY")

Which is most useful when you want to do something like:

  1. unescapeQuotedValues, third arg: true in order to unescape/parse quoted (single or double) values as being strings with escaped chars such as newline ("\n"), but also handles unicode chars (e.g. "\u00ae" and "\U0001F680") -- note that you can always include unescaped unicode chars anyway (e.g. "日本") if your .env is in UTF-8. Also note that there is no need to escape quotes inside.

Would then be available as

    value" == System.Environment.GetEnvironmentVariable("KEY")

False would mean:

"\"quoted\\n\\tvalue\"" == System.Environment.GetEnvironmentVariable("KEY")
  1. clobberExistingVars, fourth arg: false to avoid overwriting existing environment variables
System.Environment.SetEnvironmentVariable("KEY", "really important value, don't overwrite");
DotNetEnv.Env.Load(false, false, false, false); // fourth arg false, don't overwrite existing variables
System.Environment.GetEnvironmentVariable("KEY"); // == "really important value, don't overwrite"

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section.


This project is licensed under the MIT license. See the LICENSE file for more info.